2

I want to find a word in strings, but only if it doesn't begin with a prefix.

for example.

I'd like to find all the appearances of APP_PERFORM_TASK, but only if they are not starting with a prefix of CMD_DO("

so,

CMD_DO("APP_PERFORM_TASK")   <- OK (i don't need to know about this)  
BLAH("APP_PERFORM_TASK")     <-- NOT OK, this should match my search.

I tried:

(?!CMD_DO\(")APP_PERFORM_TASK

But that doesn't produce the results I need. What I doing wrong?

anubhava
  • 664,788
  • 59
  • 469
  • 547
JasonGenX
  • 5,412
  • 23
  • 86
  • 164
  • What language / engine? – Explosion Pills Oct 07 '13 at 19:22
  • I'm just looking for the regex expression. nothing particular. – JasonGenX Oct 07 '13 at 19:24
  • But what you have appears to work: http://rubular.com/r/eKNxWoSLf8 – Explosion Pills Oct 07 '13 at 19:26
  • 1
    And alternatively, possible duplicate of [this](http://stackoverflow.com/questions/899422/regular-expression-for-a-string-that-does-not-start-with-a-sequence), [this](http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word), [this](http://stackoverflow.com/questions/17095580/oracle-regex-does-not-start-with-and-does-not-end-with), [this](http://stackoverflow.com/questions/2116328/regexp-matching-string-not-starting-with-my), [this](http://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string) ... – O. R. Mapper Oct 07 '13 at 19:30
  • @ExplosionPills Lol, add the `i` flag and see what happens ;) – Jerry Oct 07 '13 at 19:55
  • @RM1970 It's important to know the language or engine of the regex, because different ones support different features. For instance, the negative lookbehind is not always supported. – Jerry Oct 07 '13 at 19:58
  • Perhaps that's why. Let's concentrate on command line tool grep – JasonGenX Oct 07 '13 at 20:03

6 Answers6

1

Try replacing NegativeLookAhead (?!) with NegativeLookBehind (?<!) in your regex

(?<!CMD_DO\(")APP_PERFORM_TASK

Check this in action here

jkshah
  • 10,615
  • 6
  • 32
  • 43
1

Based on your comment: Let's concentrate on command line tool grep

Here is grep solution without using -P switch (perl like regex):

grep 'APP_PERFORM_TASK' file | grep -v '^CMD_DO("'

Here is grep solution using -P switch and negative lokbehind:

grep -P '(?<!^CMD_DO\(")APP_PERFORM_TASK' file
anubhava
  • 664,788
  • 59
  • 469
  • 547
1

Here's a quick way:

Use the --invert-match (also known as -v) flag to ignore CMD_DO and pipe the results to a second grep that only matches BLAH:

grep -v CMD_DO dummy | grep BLAH
Spaceghost
  • 6,057
  • 3
  • 25
  • 41
0

Try this

(?!CMD_DO\(").*APP_PERFORM_TASK.*
Qsebas
  • 273
  • 2
  • 15
0

To handle an input line with both the desirable and undesirable forms like:

CMD_DO("APP_PERFORM_TASK") BLAH("APP_PERFORM_TASK")

you'd need something like this in awk (using GNU awk for gensub()):

awk -v s="APP_PERFORM_TASK" 'gensub("CMD_DO\\(\\""s,"","") ~ s' file

i.e. get rid of all of the unwanted occurrences of the string then test whats left.

Ed Morton
  • 157,421
  • 15
  • 62
  • 152
0

An awk version

awk '/APP_PERFORM_TASK/ && !/^CMD_DO/' file
Jotne
  • 38,154
  • 10
  • 46
  • 52