Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match even number of letters

Tags:

python

regex

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         # match
fsfaAAasdf # match
sAfA       # match
sdAAewAsA  # match
AeAiA      # no match

An even number of As SHOULD match.

like image 372
Andriy Drozdyuk Avatar asked Jan 11 '10 21:01

Andriy Drozdyuk


1 Answers

Try this regular expression:

^[^A]*((AA)+[^A]*)*$

And if the As don’t need to be consecutive:

^[^A]*(A[^A]*A[^A]*)*$
like image 90
Gumbo Avatar answered Oct 16 '22 01:10

Gumbo