[TriLUG] regex: match lines NOT containing X

Alexey Toptygin alexeyt at freeshell.org
Wed Feb 22 10:26:14 EST 2012


On Wed, 22 Feb 2012, Kevin Hunter wrote:

> 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?

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 technique quickly becomes unwieldly as the size of the string that 
must not match grows...

 			Alexey



More information about the TriLUG mailing list