在正则表达式的上下文中,“懒惰"和“贪婪"是什么意思? [英] What do 'lazy' and 'greedy' mean in the context of regular expressions?

查看:91
本文介绍了在正则表达式的上下文中,“懒惰"和“贪婪"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用一种可以理解的方式解释这两个术语吗?

Could someone explain these two terms in an understandable way?

推荐答案

Greedy将消耗尽可能多的东西.在 http://www.regular-expressions.info/repeat.html 中,我们看到了尝试将HTML标签与<.+>匹配的示例.假设您具有以下条件:

Greedy will consume as much as possible. From http://www.regular-expressions.info/repeat.html we see the example of trying to match HTML tags with <.+>. Suppose you have the following:

<em>Hello World</em>

您可能会认为<.+>(.表示任何非换行符,而+表示一个或多个)只会与<em></em>,实际上它会非常贪婪,并且从第一个<到最后一个>.这意味着它将匹配<em>Hello World</em>而不是您想要的.

You may think that <.+> (. means any non newline character and + means one or more) would only match the <em> and the </em>, when in reality it will be very greedy, and go from the first < to the last >. This means it will match <em>Hello World</em> instead of what you wanted.

使它变懒(<.+?>)将防止这种情况.通过在+之后添加?,我们告诉它重复尽可能少,因此遇到的第一个>是我们要停止匹配的位置.

Making it lazy (<.+?>) will prevent this. By adding the ? after the +, we tell it to repeat as few times as possible, so the first > it comes across, is where we want to stop the matching.

我建议您下载 RegExr ,这是一个很好的工具,可帮助您探索正则表达式-我一直都在用.

I'd encourage you to download RegExr, a great tool that will help you explore Regular Expressions - I use it all the time.

这篇关于在正则表达式的上下文中,“懒惰"和“贪婪"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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