如何在 bash 中正确转义这个正则表达式?

发布于 2024-12-26 10:02:01 字数 1335 浏览 0 评论 0原文

我正在尝试使用以下正则表达式运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瞄了个咪的 2025-01-02 10:02:01

在其核心,您需要用 '' 引用整个字符串。 (如果您用 "" 括起来,! 会给您带来悲伤)。然后,您只需在正则表达式(如果有)中转义内部 ' 即可。

您还需要 -P (perl) 而不是 -E (egrep) 正则表达式。

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

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]*\"' ./
草莓酥 2025-01-02 10:02:01

尝试使用 -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.

夏末染殇 2025-01-02 10:02:01

只要表达式中没有 '' 引用就可以完美工作。而且你没有,所以应该没问题。

如果您真的对引用很偏执,请将表达式放入文件中并使用 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.

九厘米的零° 2025-01-02 10:02:01

它工作得很好,只是避免使用冲突的选项。 -

[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";

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文