[英]Sed capture group not working
This seems like one of the sipmlest possible examples of a sed
capture group, and it doesn't work. 这似乎是sed
捕获组的最精妙的例子之一,并且它不起作用。 I've tested the regex in online regex testers, and does what I want. 我已经在在线正则表达式测试器中测试了正则表达式,并且做了我想要的。 At the linux command line it does not. 在linux命令行上则没有。
$ echo "a 10 b 12" | sed -E -n 's/a ([0-9]+)/\1/p'
$
and 和
$ echo "a 10 b 12" | sed -E -n 's/a ([0-9]+)/\1/p'
10 b 12
https://regex101.com/r/WS3lG9/1 https://regex101.com/r/WS3lG9/1
I would expect the "10" to be captured. 我希望捕获到“ 10”。
Your sed
pattern is not matching complete line as it is not consuming remaining string after your match ie a [0-9]+
. 您的sed
模式不匹配整行,因为它不消耗匹配后的剩余字符串,即a [0-9]+
。 That's the reason you see remaining text in output. 这就是您在输出中看到剩余文本的原因。
You can use: 您可以使用:
echo "a 10 b 12" | sed -E -n 's/a ([0-9]+).*/\1/p'
10
Or just: 要不就:
echo "a 10 b 12" | sed -E 's/a ([0-9]+).*/\1/'
10
除了使用sed
代替字符串,您还可以使用grep
找出匹配项
echo "a 10 b 12" | grep -Po '(?<=a )\d+'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.