user2696287
user2696287

Reputation: 335

Double letter matching using python regex

I am trying to find a way using regex to match words that have 3 unique sets of double letters. so far i have this:

r".*([a-z])\1.*([a-z])\2.*([a-z])\3.*"    

But that doesn't account for unique sets for double letters. Thanks in advance =)

Upvotes: 3

Views: 1369

Answers (2)

ogre
ogre

Reputation: 1

(?=expr) is a non-consuming regular expression however (?!expr) is also a non-consumer expression. This time a not equals in place of an equals.

So enclosing the 'not' in the 'equals' adds nothing. It works without that as well. However stacking non-consuming expressions does not always work, and a single non-consuming will do the job anyway by using an 'or' ('|' character).

so

r".([a-z])\1.(?!\1)([a-z])\2.(?!\1|\2)([a-z])\3."

Tidied some braces also. I think this is cleaner and will be more reliable between versions.

Upvotes: 0

Tigran Saluev
Tigran Saluev

Reputation: 3582

Maybe like this? Seems to work for me.

r".*([a-z])\1.*((?=(?!\1))[a-z])\2.*((?=(?!\1))(?=(?!\2))[a-z])\3.*"

(?=expr) is a non-consuming regular expression, and (?!expr) is regex NOT operator.

Upvotes: 4

Related Questions