grep和perl regex之间的区别? [英] Difference between grep and perl regex?

查看:169
本文介绍了grep和perl regex之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对grep的正则表达式和perl的正则表达式有所不同。考虑以下小测试:

I have a problem with what I think is a difference in grep's regex and perl's regex. Consider the following little test:

$ cat testfile.txt 
A line of text
SOME_RULE = $(BIN)
Another line of text

$ grep "SOME_RULE\s*=\s*\$(BIN)" testfile.txt 
SOME_RULE = $(BIN)

$ perl -p -e "s/SOME_RULE\s*=\s*\$(BIN)/Hello/g" testfile.txt
A line of text
SOME_RULE = $(BIN)
Another line of text

可以看到,使用正则表达式SOME_RULE \s * = \ s * \ $(BIN),grep可以找到匹配,但是perl无法使用相同的表达式来更新文件。我应该如何解决这个问题?

As you can see, using the regex "SOME_RULE\s*=\s*\$(BIN)", grep could find the match, but perl was unable to update the file using the same expression. How should I solve this problem?

感谢您的阅读并提前感谢您提供任何帮助,感谢!

Thanks for reading and thanks in advance for any help, appreciate it!

推荐答案

Perl希望'('和')'能够被转义。另外,shell在'$'上吃'\',所以你需要:

Perl wants the '(' and ')' to be escaped. Also, the shell eats the '\' on the '$', so you need:


$ perl -p -e "s/SOME_RULE\s*=\s*\\$\(BIN\)/Hello/g" testfile.txt



(或者使用单引号 - 这在任何情况下都是非常明智的。)

(or use single quotes--which is highly advisable in any case.)

这篇关于grep和perl regex之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆