OSX sed 正则表达式删除 [英] OSX sed regex removal

查看:78
本文介绍了OSX sed 正则表达式删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 grep 搜索中删除一个字符串,尽管正则表达式看起来是正确的,但我似乎无法让 sed 工作.我也在使用 OSX,但我安装了 gnu-sed 以使事情变得更简单.

I am trying to remove a string from a grep search, and I can't seem to get sed to work, despite the regex appearing to be correct. I am also using OSX, but I installed gnu-sed to make things a little easier.

我希望以下正则表达式可以消除所有最小的确定性:

I was hoping the following regex would remove all min certainties from :

gsed -r 's/minCertainty=\"\d\.\d{1,}\"//g'

这是我正在处理的文本示例,用于上下文:

Here is an example of the text I am dealing with, for context:

<vendor="company1" minCertainty="1.0"/>
<vendor="company2" minCertainty="0.75"/>
<vendor="company3" minCertainty="0.25"/>

推荐答案

\d 用于数字是 标准正则表达式语法(基本版或扩展版).gsed -r 使用扩展的正则表达式语法,不包括 perl 扩展.所以使用 [0-9][[:digit:]] 代替.此外,您不需要对表达式中的单引号进行转义(它似乎没有伤害,但我建议不要这样做).因此,请使用其中之一:

\d for digits is a perl extension to the standard regex syntax (either the basic or extended version). gsed -r uses the extended regex syntax, and doesn't include perl extensions. So use [0-9] or [[:digit:]] instead. Also, you don't need to escape the single-quotes in the expression (it doesn't seem to hurt, but I'd recommend against it). So use one of these:

gsed -r 's/minCertainty="[0-9]\.[0-9]{1,}"//g'
gsed -r 's/minCertainty="[[:digit:]]\.[[:digit:]]{1,}"//g'

这篇关于OSX sed 正则表达式删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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