I want to capture words that don't have the same letters next to each other. This one finds a letter and then checks if another letter next to it is not the same. This one works.
\b((\w)(?!(\2)))+\b
This one says do not match words that have the same letters next to each other, at least that's what I think it should do, but it doesn't work.
\b(?!((\w)\3))+\b
The second pattern \b(?!((\w)\3))+\b
is very odd
There is no \3
because the negative look-ahead (?!...)
doesn't form a capture group, so you have just two groups
If the overall pattern matches then you know the negative look-ahead hasn't matched, so \1
and \2
will never be defined
You are asking for multiple copies of a zero-width pattern (a negative look-ahead matches a single point in a string)
The negative look-ahead will never consume any characters, so there can never be any characters between the two word boundaries
It's hard to know how to "fix" this for you as I can't see the algorithm that you were trying to implement. You first pattern doesn't need the parentheses around \2
: just (?!\2)
is fine, and you don't capture the whole word to use after the match, which is usually a better way to go than using the whole matched string
I think I would write this
\b((?:(\w)(?!\2))+)\b
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments