Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-14-2006, 01:30 PM   #1
LQ Newbie
Registered: Nov 2005
Posts: 9

Rep: Reputation: 0
Regex question once again

I've searched and searched but I have not found any answer to this question.

How do you create a negation using regular expressions? Lets's assume that I have a list of words and I want to search something (exp1) but skip a result if also exp2 is also anywhere in the string.

So, "anexampleexp1" is catched but "this exp1includingexp2also" is omitted.
Old 06-14-2006, 02:11 PM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,821
Blog Entries: 15

Rep: Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664
It depends a lot on what you're using. Standard negation character is the exclamation point (!).

There is an interesting negation sytnax that can be used in ksh but it doesn't work in bash for example.

However most commonly you'd do this with "grep" which has a special flag -v that says to exclude. So you would do something like:

grep exp1 list |grep -v exp2

The first grep returns all lines that contain exp1. They are then piped into the second grep which eliminates all lines that contain exp2. The final result would be only those lines that contain exp1 but NOT exp2.

PS: Past tense of catch is caught rather than catched. One of those fun English words that makes no sense.
Old 06-14-2006, 03:15 PM   #3
LQ Newbie
Registered: Nov 2005
Posts: 9

Original Poster
Rep: Reputation: 0
Caught, of course. :lol:

I'm using a content filtering system and all the rules are based on Perl-style regular expressions - one rule per line.

So, I assume that if the first rule is (?!exp2) and I've declared later a rule exp1, the possible string containing exp1 is not filtered if also exp2 is found anywhere in the string that is being processed?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
egrep/grep regex question buldir Programming 7 05-08-2006 08:17 PM
regex question Toadman Linux - General 0 12-30-2005 01:59 PM
sed / regex question whysyn Linux - General 3 06-28-2005 03:11 PM
grep (possibly regex) question. mwtheobald Linux - Newbie 1 08-17-2002 04:05 PM
perl regex question JustinHoMi Programming 5 03-20-2002 08:46 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:31 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration