是否使用 GNU sed 在正则表达式中转义 (和) [英] Whether to escape ( and ) in regex using GNU sed

查看:16
本文介绍了是否使用 GNU sed 在正则表达式中转义 (和)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到这个网站上的几个帖子说使用 gnu sed 你应该在正则表达式中使用 () 而不是 (> 和 ).但后来我查看了 gnu sed 手册并看到他们指定必须使用 () .怎么了?

I've noticed several posts on this site which say that with gnu sed you should use ( and ) in regex rather than ( and ). But then I looked in the gnu sed manual and saw that they specify that ( and ) must be used. What's up?

推荐答案

感谢 Rocker、murga 和 chris.你们每个人都帮助我理解了这个问题.我在这里回答我自己的问题是为了(希望)将整个故事放在一个地方.

Thanks to rocker, murga, and chris. Each of you helped me understand the issue. I'm answering my own question here in order to (hopefully) put the whole story together in one place.

sed 有两个主要版本在使用:gnu 和 bsd.它们都要求在用于分组时转义基本正则表达式中的括号,但在扩展正则表达式中使用时不转义.它们的区别在于 -r 选项为 gnu 启用扩展正则表达式,而 -E 为 bsd 启用.

There are two major versions of sed in use: gnu and bsd. Both of them require parens in basic regex to be escaped when used for grouping but not escaped when used in extended regex. They diff in that the -r option enables extended regex for gnu but -E does so for bsd.

mac OSX 中的标准 sed 是 bsd.我相信世界其他地方的大部分地区都使用 gnu sed 作为标准,但我不知道谁在使用什么.如果您不确定使用的是哪个,请尝试:

The standard sed in mac OSX is bsd. I believe much of the rest of the world uses gnu sed as the standard but I don't know precisely who uses what. If you are unsure which you are using try:

> sed -r

如果你得到一个

> sed: illegal option -- r

回复你有bsd.

这篇关于是否使用 GNU sed 在正则表达式中转义 (和)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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