LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regex question once again (https://www.linuxquestions.org/questions/linux-newbie-8/regex-question-once-again-454732/)

Isotonik 06-14-2006 12:30 PM

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.

MensaWater 06-14-2006 01:11 PM

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.

Isotonik 06-14-2006 02:15 PM

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?


All times are GMT -5. The time now is 12:11 PM.