在回答另一个问题时,我引用man grep
了关于-x
-x, --line-regexp
Select only those matches that exactly match the whole line.
For a regular expression pattern, this is like parenthesizing
the pattern and then surrounding it with ^ and $.
Run Code Online (Sandbox Code Playgroud)
我已经知道这-x
就像“用^
和”包围[正则表达式] $
,但为什么它还暗示“将模式括起来”?
我想不出为什么括号是必要的,甚至无法改变任何东西。如果整个模式都用括号括起来,你就不能用捕获组在内部引用它。grep
抱怨尾随反斜杠,因此附加的)
也没有奇怪的行为(我也不希望这符合选项的精神)。
为什么man grep
特别提到parenthesizing the pattern
了-x
?
一个重要的例子是带有替代项的正则表达式:
grep -E 'this|that'
Run Code Online (Sandbox Code Playgroud)
如果你只添加^
并且$
没有括号,这将变成
grep -E '^this|that$'
Run Code Online (Sandbox Code Playgroud)
它匹配以“this”开头或以“that”结尾的行,而不是只包含“this”或“that”的行。
归档时间: |
|
查看次数: |
152 次 |
最近记录: |