Hello,
I haven’t grokked how to include an exception to a condition in a
regular expression. For example, I can’t express this correctly:
aString matches if it has a sequence of 3 or more consonants, except
when the consonants [ng] is in the sequence (in that order, n followed
by g).
Thus: plants will match, but bangking will not match.
Thanks for the help,
basi
basi wrote:
Hello,
I haven’t grokked how to include an exception to a condition in a
regular expression. For example, I can’t express this correctly:
aString matches if it has a sequence of 3 or more consonants, except
when the consonants [ng] is in the sequence (in that order, n followed
by g).
Thus: plants will match, but bangking will not match.
You might find negative look-ahead helpful. For example:
consonant = ‘[bcdfghjklmnpqrstvwxyz]’
r = /#{consonant}{3,}(?!.*ng)/
print "plants: ", r =~ “plants”, “\n”
print "bangking: ", r =~ “bangking”, “\n”
Thanks much! I haven’t tried it yet, but it looks so elegant it must
work.
basi
basi wrote:
Thanks much! I haven’t tried it yet, but it looks so elegant it must
work.
One thing I screwed up: If you want to avoid strings that contain ng
anywhere, do the negative look-ahead before you match the consonants:
consonant = ‘[bcdfghjklmnpqrstvwxyz]’
r = /(?!.*ng)#{consonant}{3,}/
Yes, this one catches initial and final occurences as well. Much
appreciated.
basi