grep -o 正则表达式的问题

Aco*_*orn 5 grep regular-expression

我正在尝试使用带有 --only-matching 标志的 grep 命令,但它的行为不像我期望的那样。

这个命令:

echo "1/2/3/4/5" | grep -oE "^([^/]+/){0,2}"

给出这个输出:

1/2/
3/4/
Run Code Online (Sandbox Code Playgroud)

我只是期待 1/2/

这是怎么回事..?3/4/不应该匹配,"^([^/]+/){0,2}"因为它不在行的开头..

运行 GNU grep 2.5.1

zar*_*dav 8

这是早于此提交(即早于version )的版本中的错误GNU GrepGNU2.5.3

引用变更日志的相关部分:

Previously failing tests relative to left anchors (^ and \<) and -w should now pass.
Run Code Online (Sandbox Code Playgroud)

描述错误的初始提交还为其添加了一个测试:

# End of a previous match should not match a "start of ..." expression.
grep_test "word_word/" "word_/" "^word_*" -o
Run Code Online (Sandbox Code Playgroud)

  • 这个错误,其中`grep -o`/`grep --only-matching` 允许插入符在前一个匹配结束后而不是仅在行首匹配,在 Mac OS X 10.9.4 中仍然存在。 (3认同)