替换正则表达式不适用于sed(Replace regex does not work with sed)

编程入门 行业动态 更新时间:2024-10-26 04:33:00
替换正则表达式不适用于sed(Replace regex does not work with sed)

我试图替换此末尾的数字: version=12在文件config.file

这是我的脚本:

nbr=13 sed -re "s/version=\d+/$nbr/g" config.file

正则表达式似乎没有检测到数字。

有人有什么想法吗?

I am trying to replace the number at the end of this : version=12 containted in the file config.file

Here is my script :

nbr=13 sed -re "s/version=\d+/$nbr/g" config.file

The regex does not seem to detect the number.

Have someone any ideas why ?

最满意答案

您可以使用

nbr=13 sed -re "s/(version=)[0-9]+/\1$nbr/g" config.file

请参阅在线演示

即使您使用的是ERE POSIX正则表达式(使用-r启用),您仍然无法使用类似\d数字模式的PCRE。

此外,我添加了一个捕获组(version=) ,以便能够使用\1反向引用在替换部分中恢复它。

You can use

nbr=13 sed -re "s/(version=)[0-9]+/\1$nbr/g" config.file

See the online demo

Even if you are using an ERE POSIX regex (enabled with -r), you still cannot use PCRE like \d digit pattern.

Also, I added a capturing group around (version=) to be able to restore it in the replacement part with a \1 backreference.

更多推荐

本文发布于:2023-08-05 05:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   正则表达式   sed   work   regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!