背景
工作中遇到正则表达式匹配失误的问题,虽最终解决了问题。但讨论过程中提到了贪婪和非贪婪,
大家都不是讲的十分清楚。随后做了一些了解,记录如下。
贪婪?非贪婪?
简言之贪婪就是尽量找最长的匹配字符串,非贪婪尽量找最短的匹配字符串。
正则表达式中的 “?” 作为量词使用时,代表的是匹配“0次或1次”。但“?”跟在量词后面就表示以非贪婪模式匹配。
正则表达式常用量词如下:
* 匹配0次或多次
? 匹配0次或1次
+ 匹配1次或多次
{m,n} 匹配大于等于m次,小于等于n次 (n>m)
{m,} 匹配大于等于m次
Example
我们用 regexp:abcd123 这个字符串来做一下例子
Example1:
{m,n} 与 {m,n}?
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]{3,5}";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为: regex
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]{3,5}?";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为: reg
Example2:
{m,}与{m,}?
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]{3,}";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为: regexp
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]{3,}?";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为: reg
Example3:
与?
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]*";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为: regexp
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]*?";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为:
Example4:
+与+?
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]+";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为: regex
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]+?";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为: r
Example4:
?与??
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]?";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为: r
public class RegExpTester{
public static void main(String[] args){
String str = "regexp:abcd123efg";
String regexp = "[a-z]??";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}
}
}
//打印结果为:
网友评论