I need to match an expression in Python with regular expressions that only matches even number of letter occurrences. For example:
AAA # no match
AA
'A*' means match any number of A's. Even 0.
Here's how to match a string with an even number of a's, upper or lower:
re.compile(r'''
^
[^a]*
(
(
a[^a]*
){2}
# if there must be at least 2 (not just 0), change the
# '*' on the following line to '+'
)*
$
''',re.IGNORECASE|re.VERBOSE)
You probably are using a as an example. If you want to match a specific character other than a, replace a with %s and then insert
[...]
$
'''%( other_char, other_char, other_char )
[...]