最近在写程序时,碰到一个场景,需要找到一个字符串中指定的一个片段,而不是所有片段,这就涉及到正则表达式中贪婪和非贪婪两种模式。


字面意思上,正则表达式一般趋向于最大长度匹配,就是贪婪模式。匹配到结果就好,就少的匹配字符,就是非贪婪模式

直接上个例子,

String str="abcaxc";
Patter p="ab.*c";

如果是贪婪模式,上面使用模式p匹配字符串str,结果就是匹配到:abcaxc,匹配到了所有的字符串。

如果是非贪婪模式,上面使用模式p匹配字符串str,结果就是匹配到:abc,只匹配到了部分的字符串。

编程中怎样区分这两种模式?

默认情况下,正则用的都是贪婪模式,如果要使用非贪婪模式,需要在量词后面直接加上一个问号"?",量词包括如下,

(1) {m,n}:m到n个。

(2) *:任意多个。

(3) +:一个到多个。

(4) ?:0或一个。

再上个程序,用贪婪和非贪婪模式找到content中的内容,

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegularTest {
  public static void main(String[] arg) {
    String text = "(content:\"hello root\";hello:\"word\";)";
    String rule1 = "content:\".+\"";  // 贪婪模式
    String rule2 = "content:\".+?\""; // 非贪婪模式


    System.out.println("文本:" + text);
    System.out.println("贪婪模式:" + rule1);
    Pattern p1 = Patternpile(rule1);
    Matcher m1 = p1.matcher(text);
    while (m1.find()) {
      System.out.println("匹配结果:" + m1.group(0));
    }


    System.out.println("非贪婪模式:" + rule2);
    Pattern p2 = Patternpile(rule2);
    Matcher m2 = p2.matcher(text);
    while (m2.find()) {
      System.out.println("匹配结果:" + m2.group(0));
    }
  }
}

如果是贪婪模式,返回两个字符串,而非贪婪模式,则只返回第一个,

文本:(content:"hello root";hello:"word";)
贪婪模式:content:".+"
匹配结果:content:"hello root";hello:"word"
非贪婪模式:content:".+?"
匹配结果:content:"hello root"

针对不同场景,我们就可以选择合适的模式。

参考资料,

https://wwwblogs/xudong-bupt/p/3586889.html

近期更新的文章:

《Facebook宕机的经验

《想,都是问题,做,才是答案

《即将“再次”免费的JDK

《"裸金属"是什么?

《数据管理,数据治理,数据管控

文章分类和索引:

《公众号800篇文章分类和索引

更多推荐

正则表达式的贪婪和非贪婪模式