[TriLUG] regex: match lines NOT containing X

Len Boyle Len.Boyle at sas.com
Wed Feb 22 10:13:49 EST 2012


$ cat tempe
If two heads are better than one,
are two keyboards better than one?
Would you say the same if it were two hydra heads?
$ grep -v heads tempe
are two keyboards better than one?
$

-----Original Message-----
From: trilug-bounces at trilug.org [mailto:trilug-bounces at trilug.org] On Behalf Of Kevin Hunter
Sent: Wednesday, February 22, 2012 9:56 AM
To: Triangle Linux Users Group General Discussion
Subject: [TriLUG] regex: match lines NOT containing X

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
--
This message was sent to: len.boyle at sas.com <len.boyle at sas.com> To unsubscribe, send a blank message to trilug-leave at trilug.org from that address.
TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
Unsubscribe or edit options on the web	: http://www.trilug.org/mailman/options/trilug/len.boyle%40sas.com
TriLUG FAQ          : http://www.trilug.org/wiki/Frequently_Asked_Questions





More information about the TriLUG mailing list