How can I avoid this regex in bash?

I am trying to run grep with the following regex:

(?<!key:)(?<!orKey:)(?<!isEqualToString:)\@\"[A-Za-z0-9]*\" 

First try:

 $ grep -r -n -H -E (?<!key:)(?<!orKey:)(?<!isEqualToString:)\@\"[A-Za-z0-9]*\" ./ -bash: !key: event not found 

Ok, so I need to avoid the "!" s ...

 $ grep -r -n -H -E (?<\!key:)(?<\!orKey:)(?<\!isEqualToString:)\@\"[A-Za-z0-9]*\" ./ -bash: syntax error near unexpected token `(' 

Ok, so I need to get away from "(" s ...

 $ grep -r -n -H -E \(?<\!key:\)\(?<\!orKey:\)\(?<\!isEqualToString:\)\@\"[A-Za-z0-9]*\" ./ -bash: !key:)(?: No such file or directory 

So, do I need to quote a line?

 $ grep -r -n -H -E '\(?<\!key:\)\(?<\!orKey:\)\(?<\!isEqualToString:\)\@\"[A-Za-z0-9]*\"' ./ 

It returns no results ... but I tried a simpler regular expression that has no statements with a negative expression, and it works fine ... I also used TextWrangler with this regular expression, and it really works, so I can only assume that I am doing something wrong on the command line here.

EDIT:

If I use the -p option:

 $ grep -r -n -H -E -P '\(?<\!key:\)\(?<\!orKey:\)\(?<\!isEqualToString:\)\@\"[A-Za-z0-9]*\"' ./ grep: conflicting matchers specified 

An example of the contents of a file that must match:

 NSString * foo = @"bar"; 

An example of the contents of a file that should NOT match:

 return [someDictonary objectForKey:@"foo"]; 
+4
source share
4 answers

At the heart of this, you must specify the entire string with '' (If you attach "" , ! Will give you sadness). Then you only need to avoid the inner ' inside your regular expression (if any).

Also you want -P (perl) instead of -E (egrep) regex.

 grep -r -n -H -P '(?<!key:)(?<!orKey:)(?<!isEqualToString:)\@\"[A-Za-z0-9]*\"' ./ 
+5
source

try using the -P option, which will interpret the regex as a perl regex. Also, if you provide some examples of input and output, this will help to get an answer.

+4
source

' quoting works fine while there isn’t in the expression. ' And you don’t have them, so everything should be fine.

If you are really paranoid about quoting, put the expression in a file and use grep -f FILENAME instead to read the regular expression from the file.

But the real problem may be that you need to specify grep -P to explicitly request Perl regular expression support.

+2
source

It works great, just don't use conflicting options. -

 [jaypal:~/Temp] cat filename NSString * foo = @"bar"; return [someDictonary objectForKey:@"foo"]; [jaypal:~/Temp] grep -P '(?<!key:)(?<!orKey:)(?<!isEqualToString:)\@\"[A-Za-z0-9]*\"' filename NSString * foo = @"bar"; 
0
source

Source: https://habr.com/ru/post/1390152/


All Articles