正则表达式很懒,应该贪心 [英] Regex is behaving lazy, should be greedy

查看:87
本文介绍了正则表达式很懒,应该贪心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为默认情况下,我的 Regex 会表现出我想要的贪婪行为,但是它不在以下代码中:

I thought that by default my Regex would exhibit the greedy behavior that I want, but it is not in the following code:

 Regex keywords = new Regex(@"in|int|into|internal|interface");
 var targets = keywords.ToString().Split('|');
 foreach (string t in targets)
    {
    Match match = keywords.Match(t);
    Console.WriteLine("Matched {0,-9} with {1}", t, match.Value);
    }

输出:

Matched in        with in
Matched int       with in
Matched into      with in
Matched internal  with in
Matched interface with in

现在我意识到,只要我按长度降序对关键字进行排序,就可以使它适用于这个小示例,但是

Now I realize that I could get it to work for this small example if I simply sorted the keywords by length descending, but

  • 我想了解为什么 不能按预期工作,并且
  • 我正在从事的实际项目 正则表达式中还有更多的单词, 重要的是要把它们留在里面 字母顺序.
  • I want to understand why this isn't working as expected, and
  • the actual project I am working on has many more words in the Regex and it is important to keep them in alphabetical order.

所以我的问题是:为什么这很懒?我该如何解决?

So my question is: Why is this being lazy and how do I fix it?

推荐答案

懒惰和贪婪仅适用于量词(?*+{min,max}).交替总是按顺序匹配,然后尝试第一个可能的匹配.

Laziness and greediness applies to quantifiers only (?, *, +, {min,max}). Alternations always match in order and try the first possible match.

这篇关于正则表达式很懒,应该贪心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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