Java 正则表达式匹配模式(贪婪型、勉强型、占有型)
(编辑:jimmy 日期: 2024/11/18 浏览:3 次 )
Greediness(贪婪型):最大匹配
X"htmlcode">
String test = "a<tr>aava </tr>abb "; String reg = "<.+>"; System.out.println(test.replaceAll(reg, "###"));
输出:a###abb
Reluctant(Laziness)(勉强型):最小匹配
X"htmlcode">
String test = "a<tr>aava </tr>abb "; String reg = "<.+"; System.out.println(test.replaceAll(reg, "###"));
输出:a###aava ###abb
与 Greediness 不同,Reluctant 模式下匹配了两次内容
Possessive(占有型):完全匹配
X"htmlcode">
String test = "a<tr>aava </tr>abb "; String reg = "<.++>"; String test2 = "<tr>"; String reg2 = "<tr>"; System.out.println(test.replaceAll(reg, "###")); System.out.println(test2.replaceAll(reg2, "###"));
输出:a<tr>aava </tr>abb
下一篇:正则表达式小脚本(对输入内容进行处理)