假设判刑
“时间现在是晚上8:15。其次是其他一些文字”
为什么以下正则表达式与该行不匹配?
egrep '\<[1-9]\s*:\s*[0-9]{2}\s*[Pp][Mm]\>' file
我在MAC上,使用GNU egrep
$ egrep --version egrep(GNU grep)2.5.1
答案 0 :(得分:2)
这是因为正则表达式中的前导和尾随符号:&lt;和&gt;。拿出那些,这是一场比赛。
答案 1 :(得分:2)
完全无法重现。
Mac OS X 10.7:
(0)marks-mac-mini> cat file
The time is now 8:15pm. Followed by some other text
"The time is now 8:15pm. Followed by some other text"
(0)marks-mac-mini> egrep '\<[1-9]\s*:\s*[0-9]{2}\s*[Pp][Mm]\>' file
The time is now 8:15pm. Followed by some other text
"The time is now 8:15pm. Followed by some other text"
(0)marks-mac-mini> egrep --version
egrep (GNU grep) 2.5.1
所以你在某个地方做错了......
编辑啊,问题是空间。 egrep无法识别\s
;那是一种Perlism。你的问题不包括那里的空间。
答案 2 :(得分:1)
在我的OS X机器上使用GNU egrep,表达式可以很好地匹配你的行。它可能是你特定版本的grep(它是非GNU grep吗?)