如何在 bash 中正确转义此正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8797829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I properly escape this regex in bash?
提问by Steve
I'm trying to run grep with the following regex:
我正在尝试使用以下正则表达式运行 grep:
(?<!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.
不返回任何结果......但我尝试了一个更简单的正则表达式,它没有负向后断言,并且运行良好......我还使用 TextWrangler 和这个正则表达式并且它确实有效,所以我只能假设我在这里的命令行上做错了。
EDIT:
编辑:
If I use the -poption:
如果我使用该-p选项:
$ 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"];
回答by mathematical.coffee
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) 而不是-E(egrep) 正则表达式。
grep -r -n -H -P '(?<!key:)(?<!orKey:)(?<!isEqualToString:)\@\"[A-Za-z0-9]*\"' ./
回答by ennuikiller
try using the -Poption 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.
尝试使用-P选项,它将正则表达式解释为 perl 正则表达式。此外,如果您提供一些示例输入和输出,它将有助于获得答案。
回答by Aaron McDaid
'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 FILENAMEinstead, so that it reads the regex from the file.
如果您真的对引用感到偏执,请将表达式放入文件中并grep -f FILENAME改为使用,以便它从文件中读取正则表达式。
But the real issue might be that you need to specify grep -Pto explicitly ask for the Perl regular expression support.
但真正的问题可能是您需要指定grep -P显式请求 Perl 正则表达式支持。
回答by jaypal singh
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";

