regex - sed 4.2.2 中的数字匹配错误?

标签 regex linux bash ubuntu sed

我不是 sed 的新手,但我也很难相信这个古老的产品中有一个可怕的错误,所以我只想通过更多的眼睛来检查我是否在做一些愚蠢的事情在上面。

我试图从 URL 字符串中提取一组数字,但 sed 似乎匹配每个字符以及数字范围。

bdetweiler@HPSin:~$ echo "www.blah.com/012345/moreblah.html" | sed -e 's/\([[:digit:]]*\)/!\1/g'
!w!w!w!.!b!l!a!h!.!c!o!m!/!012345/!m!o!r!e!b!l!a!h!.!h!t!m!l!
bdetweiler@HPSin:~$ echo "www.blah.com/012345/moreblah.html" | sed -e 's/\([0-9]*\)/!\1/g'
!w!w!w!.!b!l!a!h!.!c!o!m!/!012345/!m!o!r!e!b!l!a!h!.!h!t!m!l!
bdetweiler@HPSin:~$ echo "www.blah.com/012345/moreblah.html" | sed -e 's/.*\([0-9]*\).*/!\1/g'
!
bdetweiler@HPSin:~$ echo "www.blah.com/012345/moreblah.html" | sed -e 's/.*\([[:digit:]]*\).*/!\1/g'
!


bdetweiler@HPSin:~$ sed --version 
sed (GNU sed) 4.2.2

bdetweiler@HPSin:~$ uname -a
Linux HPSin 3.13.0-62-generic #102-Ubuntu SMP Tue Aug 11 14:29:36 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

另一方面,egrep 可以很好地解决这个问题:

enter image description here

我是不是做错了什么,或者我可能看到了 sed 中的一个可怕错误?

编辑 2015-10-29 07:35 这似乎不是 sed 特有的。 Perl 给我同样的问题:

echo -n "www.blah.com/012345/moreblah.html" | perl -pe "s/.*([0-9]+).*/\1/g"
5

最佳答案

至少 1 个数字,如果不是,则所有内容都对应 :-)

echo "www.blah.com/012345/moreblah.html" | sed -e 's/\([[:digit:]]\{1,\}\)/!\1/g'

关于regex - sed 4.2.2 中的数字匹配错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33392320/

相关文章:

c++ - 通过从文件中读取来 boost 正则表达式匹配

php - 奇怪的 PHP 正则表达式 Preg_Match Bug?

regex - 如何在vim中合并奇数行和偶数行?

c - 命令行参数会是 "passed twice"吗?

linux - "grep -q"有什么意义

linux - `kill "$! "` 在 FOR 循环时不起作用

正则表达式获取组内匹配

linux - Linux下从Mono-GTK#应用程序访问 "Applications menu"

linux - 静态链接库的优缺点是什么?

Bash 等待立即终止?