Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

excluding words with consecutive repeat characters

Tags:

regex

Say I have a list of words like this: mouse, abba, roar, accent

I need a regex that will match mouse and roar, but not abba or accent, because the last two have consecutive repeat characters (bb and cc).

I managed to come up with the following pattern to match only the words WITH double consecutive characters:

.*(.)\1.*

However what I need is the opposite, but I am having a hard time inverting it.

like image 809
Daniel Scocco Avatar asked Dec 19 '22 20:12

Daniel Scocco


1 Answers

You can use this:

\b(?:([a-z])(?!\1))+\b

it use a negative lookahead (?!..) which tests that each capture is not followed by itself. To be sure that the pattern doesn't cut a word before the word end or begin in the middle of a word, I added word boundaries \b on each side.

An other way:

(?![a-z]*([a-z])\1)\b[a-z]+\b
like image 89
Casimir et Hippolyte Avatar answered Jan 11 '23 23:01

Casimir et Hippolyte