public void greedy(){
String content = "abbbb/hhh";
String regexp = "[a-z/]*/[a-z]*";
if(content.matches(regexp))
System.out.println("success");
else
System.out.println("failure");
}
第一次:
① 正则表达式中的[a-z/]* 匹配 content 中的 abbbb/
② 正则表达式中的/ 不匹配 content 中的 h
匹配失败qwq
第二次:(发生回溯)
① 正则表达式中的[a-z/]* 匹配 content 中的 abbbb
② 正则表达式中的/ 匹配 content 中的 /
③ 正则表达式中的[a-z]* 匹配 content 中的 hhh
匹配成功oh
public void greedy(){
String content = "abbbb//hhh";
String regexp = "[a-z/]*//[a-z]*";
if(content.matches(regexp))
System.out.println("success");
else
System.out.println("failure");
}
同理,这个就会回溯两次~
讲得很好的一篇文章: