Sed无法将非空白字符与字符类匹配 [英] Sed failed to match non whitespace characters with character class

查看:72
本文介绍了Sed无法将非空白字符与字符类匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取在/etc/lvm/lvm.conf 中配置的过滤规则,例如 filter = ["r |/dev/sda |"] .我希望 sed 返回"r |/dev/sda |" .因此,我尝试了以下脚本:

I want to extract filter rules configured in /etc/lvm/lvm.conf, like filter = [ "r|/dev/sda|" ]. I want sed to return "r|/dev/sda|". So I have tried the following script:

echo'filter = ["r |/dev/sda |"]'|sed -r's:^ \ s * filter \ s * = \ s * \ [\ s *([^ \ s] +)\ s * \]:\ 1:g'

但是它没有用,脚本返回了 filter = ["r |/dev/sda |"] .我已经尝试了一些在线正则表达式测试器,该组已正确匹配.

But it didn't work, the script has returned filter = [ "r|/dev/sda|" ]. I've tried a few on line regex tester, the group has been matched correctly.

但是,如果我将 [^ \ s] + 替换为.+ ,则可以.

However, if I replace [^\s]+ by .+, it works.

不是 [^ \ s] + 表示多个非空白字符吗?

请问有什么想法吗?

推荐答案

Acc.到 regular-expressions.info :

一个关键的语法差异是反斜杠不是POSIX括号表达式中的元字符.因此,在POSIX中,正则表达式 [\ d] 匹配 \ d .

因此,您需要将 [^ \ s] 替换为 [^ [:space:]] (除空格以外的任何字符)

So you need to replace [^\s] with [^[:space:]] (any char other than whitespace).

示例:

echo ' filter = [ "r|/dev/sda|" ] ' | sed -E 's:^\s*filter\s*=\s*\[\s*([^[:space:]]+)\s*\]:\1:g'

输出:"r |/dev/sda |"

这篇关于Sed无法将非空白字符与字符类匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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