【发布时间】:2014-09-06 06:39:59
【问题描述】:
我多年来一直使用正则表达式,但从未遇到过这个问题。在示例网站(如http://regexone.com/lesson/1)上,我可以尝试我正在尝试做的事情,它匹配,但在使用sed 的unix shell 中,它不匹配。我在尝试编写 logcheck 跳过规则时发现了这一点。
$ echo 'Hello, world!' | sed '/^\w\w\wlo, wor.*$/d'
$
有效,但是
$ echo 'Hello, world!' | sed '/^\w{3}lo, wor.*$/d'
Hello, world!
没有。它似乎没有看到带有 {3} 的 3 个字母数字。
我通过尝试减少发现这一点
$ echo "Jul 15 11:31:08 gateway-laptop dbus[3076]: [system] Successfully activated service 'org.freedesktop.PackageKit'"|sed "/^\w{3} [ :0-9]{11} [._[:alnum:]-]+ dbus\[[0-9]+\]: \[system\].*/d"
Jul 15 11:31:08 gateway-laptop dbus[3076]: [system] Successfully activated service 'org.freedesktop.PackageKit'
我认为应该匹配。降低这个复杂度,这不匹配
$ echo "Jul 15 11:31:08 gateway-laptop dbus[3076]: [system] Successfully activated service 'org.freedesktop.PackageKit'"|sed "/^\w{3}.*"/d
Jul 15 11:31:08 gateway-laptop dbus[3076]: [system] Successfully activated service 'org.freedesktop.PackageKit'
看起来它的行为 ^\w{3} 应该匹配该行中的前 3 个字母数字字符,然后 .* 应该匹配该行的其余部分到 EOL。
【问题讨论】:
-
更新了标签;
bash与此无关。
标签: regex sed pattern-matching match