macos OS X 上的 sed - 似乎无法在正则表达式中使用 +

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1227174/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:18:42  来源:igfitidea点击:

sed on OS X - can't seem to use + in regexps

regexmacossed

提问by stib

Now according to all the literature

现在根据所有文献

echo 1234abcd|sed "s|[0-9]\+|#|g"

should output #abcd. And

应该输出#abcd。和

echo abcd|sed "s|[0-9]\+|#|g"

should output abcd.

应该输出 abcd。

But on OS X 10.4.11 the first expression outputs 1234abcd. Using * instead of + works for the first example but fails on the second, outputting #abcd, because the [0-9] pattern is matched zero times.

但在 OS X 10.4.11 上,第一个表达式输出 1234abcd。使用 * 而不是 + 适用于第一个示例,但在第二个示例中失败,输出 #abcd,因为 [0-9] 模式匹配零次。

Does the + operator not work in regular expressions in OS X? Is there an alternative?

+ 运算符在 OS X 中的正则表达式中不起作用吗?有替代方案吗?

Thanks

谢谢

回答by paxdiablo

On OSX, sedby default uses basic REs. You should use sed -Eif you want to use modern REs, including the "+"one-or-more operator.

在 OSX 上,sed默认情况下使用基本 RE。sed -E如果您想使用现代 RE,包括"+"一个或多个运算符,则应使用。

See herefor the indication that seduses basic REs by default, herefor the modern RE syntax, and herefor the basic RE (ed) information.

请参阅此处以了解sed默认情况下使用基本 RE的指示,此处了解现代 RE 语法,以及此处了解基本 RE ( ed) 信息。



Alternatively, if you have a regular expression engine that doesn'tsupport +at all, you can simply use *instead, by converting (for example):

或者,如果您有一个根本不支持的正则表达式引擎+,您可以简单地使用*,通过转换(例如):

[a-z]+

into:

进入:

[a-z][a-z]*

回答by laalto

Obsolete basic regular expressions do not support +and ?quantifiers. They are regular characters.

过时的基本正则表达式不支持+?量词。他们是普通角色。

Alternatives for [0-9]+are e.g. [0-9]{1,}or [0-9][0-9]*.

的替代品[0-9]+是例如[0-9]{1,}[0-9][0-9]*

Or you can use sed -Eto use modern, extended regular expressions.

或者您可以使用sed -E现代的、扩展的正则表达式。

回答by Marcin

If + doesn't work, you can always use {1,}

如果 + 不起作用,您可以随时使用 {1,}

回答by ghostdog74

you can use awk

你可以使用 awk

# echo 1234abcd| awk '{gsub(/[0-9]+/,"#")}1'
#abcd

# echo abcd| awk '{gsub(/[0-9]+/,"#")}1'
abcd

回答by Rodrigo Queiro

Many of the OS X unix utilities are of versions that lack the comforts of their GNU equivalents. As Pax says, you can use -E:

许多 OS X unix 实用程序的版本缺乏 GNU 等价物的舒适性。正如Pax所说,您可以使用-E:

drigz@mbp drigz 0$ echo 1234abcd | /usr/bin/sed "s/[0-9]\+/#/g" 
1234abcd
drigz@mbp drigz 0$ echo 1234abcd | /usr/bin/sed -E "s/[0-9]+/#/g" 
#abcd

Note that small changes to the syntax of your regex are required (\+ to + in this case).

请注意,需要对正则表达式的语法进行小的更改(在本例中为 \+ 到 +)。

However, I prefer to use fink to get GNU utilities:

但是,我更喜欢使用 fink 来获取 GNU 实用程序:

drigz@mbp drigz 0$ echo 1234abcd | /sw/bin/sed "s/[0-9]\+/#/g"
#abcd
drigz@mbp drigz 0$ /sw/bin/sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.