如何在 bash 中正确转义这个正则表达式?
我正在尝试使用以下正则表达式运行 grep:
(?<!key:)(?<!orKey:)(?<!isEqualToString:)\@\"[A-Za-z0-9]*\"
第一次尝试:
$ grep -r -n -H -E (?<!key:)(?<!orKey:)(?<!isEqualToString:)\@\"[A-Za-z0-9]*\" ./
-bash: !key: event not found
好的,所以我需要转义“!”...
$ grep -r -n -H -E (?<\!key:)(?<\!orKey:)(?<\!isEqualToString:)\@\"[A-Za-z0-9]*\" ./
-bash: syntax error near unexpected token `('
好的,所以我需要转义“(”...
$ grep -r -n -H -E \(?<\!key:\)\(?<\!orKey:\)\(?<\!isEqualToString:\)\@\"[A-Za-z0-9]*\" ./
-bash: !key:)(?: No such file or directory
好的,所以我需要引用字符串?
$ grep -r -n -H -E '\(?<\!key:\)\(?<\!orKey:\)\(?<\!isEqualToString:\)\@\"[A-Za-z0-9]*\"' ./
不返回结果...但我尝试了一个更简单的正则表达式,它没有否定后向断言,并且运行良好...我还使用 TextWrangler 与此正则表达式,它确实有效,所以我只能假设我做错了什么此处的命令行。
编辑:
如果我使用-p
选项:
$ grep -r -n -H -E -P '\(?<\!key:\)\(?<\!orKey:\)\(?<\!isEqualToString:\)\@\"[A-Za-z0-9]*\"' ./
grep: conflicting matchers specified
应匹配的文件内容示例:
NSString * foo = @"bar";
不应匹配的文件内容示例:
return [someDictonary objectForKey:@"foo"];
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"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在其核心,您需要用
''
引用整个字符串。 (如果您用""
括起来,!
会给您带来悲伤)。然后,您只需在正则表达式(如果有)中转义内部'
即可。您还需要
-P
(perl) 而不是-E
(egrep) 正则表达式。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.尝试使用 -P 选项,该选项会将正则表达式解释为 perl 正则表达式。此外,如果您提供一些示例输入和输出,它将有助于获得答案。
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.
只要表达式中没有
'
,'
引用就可以完美工作。而且你没有,所以应该没问题。如果您真的对引用很偏执,请将表达式放入文件中并使用 grep -f FILENAME 代替,以便它从文件中读取正则表达式。
但真正的问题可能是您需要指定 grep -P 来显式请求 Perl 正则表达式支持。
'
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.它工作得很好,只是避免使用冲突的选项。 -
It works fine, just avoid using conflicting options. -