如何做一个非贪婪匹配在grep? [英] How to do a non-greedy match in grep?

查看:2506
本文介绍了如何做一个非贪婪匹配在grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想grep最短的匹配,模式应该是这样:

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

<car ... model=BMW ...>
...
...
...
</car>

...表示任何字符,输入为多行。

... means any character and the input is multiple lines.

推荐答案

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

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.

这篇关于如何做一个非贪婪匹配在grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆