[TriLUG] regex: match lines NOT containing X
Kevin Hunter
hunteke at earlham.edu
Wed Feb 22 11:14:19 EST 2012
At 10:26am -0500 Wed, 22 Feb 2012, Alexey Toptygin wrote:
> If you can't invert the whole regex (as in grep -v or !~ in perl),
> you can do:
>
> /^(?:[^h]|h[^e]|he[^a]|hea[^d]|head[^s])*$/
This finally set me off on the right track. I can't invert the whole
regex, but I do know that no where in the string can I match against
heads. Thanks to the above, it dawned on me that zero-width assertion
must be true for /every/ character matched. So (recopying context from
earlier message), to match only the lines that don't have heads in them
with a single regex (and exploiting some knowledge of the structure that
'two' will always come before 'heads', if 'heads' comes at all):
-----
If two heads are better than one,
are two keyboards better than one?
Would you say the same if it were two hydra heads?
-----
/^(.*two)(.(?!heads))*$/
And voila! It's the grouping of (.(?!something)) that's the key.
Thanks all!
Kevin
More information about the TriLUG
mailing list