How to do a non-greedy match in grep?

后端 未结 7 2054
深忆病人
深忆病人 2020-11-30 17:30

I want to grep the shortest match and the pattern should be something like:


...
...
...

... mean

7条回答
  •  [愿得一人]
    2020-11-30 17:49

    You're looking for a non-greedy (or lazy) match. To get a non-greedy match in regular expressions you need to use the modifier ? after the quantifier. For example you can change .* to .*?.

    By default grep doesn't support non-greedy modifiers, but you can use grep -P to use the Perl syntax.

提交回复
热议问题