且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在grep中进行非贪婪匹配?

更新时间:2023-02-15 08:02:12

您正在寻找非贪婪(或惰性)匹配.要在正则表达式中获得非贪婪匹配,您需要在量词后使用修饰符 ?.例如,您可以将 .* 更改为 .*?.

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 .*?.

默认情况下 grep 不支持非贪婪修饰符,但您可以使用 grep -P 来使用 Perl 语法.

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