4

I'm 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 escape 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 escape the "("s...

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

Ok, so I need to quote the string?

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

Returns no results... but I tried a simpler regex which doesn't have the negative-look-behind assertions, and it ran fine... I also used TextWrangler with this regex and it does work, so I can only assume I'm 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 file contents which should match:

NSString * foo = @"bar"; 

An example of file contents which should NOT match:

return [someDictonary objectForKey:@"foo"]; 
1
  • In general, quoting with '...' is far preferable to escaping. Escaping will mutilate your regex, make it less intuitive, etc. Commented Nov 30, 2017 at 22:56

4 Answers 4

5

At the core of it you need to quote the entire string with ''. (If you enclose with "" the ! will give you grief). Then you only need to escape internal ' within your regex (if any).

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

grep -r -n -H -P '(?<!key:)(?<!orKey:)(?<!isEqualToString:)\@\"[A-Za-z0-9]*\"' ./ 
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent. Thank you. That works! So, If I enclose the regex in single-ticks I don't have to escape the !s?
Yep. Otherwise within a " or if not quoted at all, ! has a special meaning in bash.
4

try using the -P option which will interpret the regex as a perl regex. Also if you provide some example input and output it would help in getting an answer.

4 Comments

Thanks for the reply. I edited my question. Like I said, the regex itself works great through TextWrangler (which uses grep internally) - so I'm pretty positive this is a bash quoting problem.
@Steve, it's quite possible that TextWranger uses grep -P internally. (checking manual now...)
I couldn't accept two answers, so best I can do is +1. Thanks for the help.
@ennuikiller You got a +1 from me too for coming first to the rescue!! :)
3

' quoting works perfectly as long as there are no ' in your expression. And you don't have any, so that should be fine.

If you are really paranoid about quoting, put the expression in a file and use grep -f FILENAME instead, so that it reads the regex from the file.

But the real issue might be that you need to specify grep -P to explicitly ask for the Perl regular expression support.

Comments

0

It works fine, just avoid using 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"; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.