[TriLUG] regex: match lines NOT containing X

Kevin Hunter hunteke at earlham.edu
Wed Feb 22 09:56:07 EST 2012


Hullo List,

An actual technical question here: Is there a way to match an entire 
line such that the entire line does *not* contain a sequence of characters?

Example input containing three lines:
-----
If two heads are better than one,
are two keyboards better than one?
Would you say the same if it were two hydra heads?
-----

My criteria is very specific: I want lines that do *not* contain 
'heads'.  My first thought was to use an assertion:

/^.*two.*(?!heads).*$/

However, this naive implementation matches both lines 2 and 3.  I've 
needed this construct more than a few times, so I'm almost sure it 
exists; I just don't know the right Google terms, apparently.  On the 
other hand, I could accept that this may not be possible in a single 
step regex.

Any ideas?

Thanks,

Kevin



More information about the TriLUG mailing list