62,612
社区成员
发帖
与我相关
我的任务
分享
public class TestRegularExpression {
/**
* @param args
*/
public static void main(String[] args) {
String testString = "Never give up! Never surrender!";
String regularException = "Never.{8,10}!";
System.out.println(regularException);
Pattern p = Pattern.compile(regularException);
Matcher m = p.matcher(testString);
int i = 0;
while (m.find()) {
System.out.println("Match \"" + m.group() + "\" at positions "
+ m.start() + "-" + (m.end() - 1));
System.out.println(m.lookingAt());
System.out.println(m.matches());
}
}
}
String testString = "Never give up! Never surrender!Never shut up!";//改成这样3次匹配将正常find()一圈后退出,如果是2个匹配的则陷入死循环
String regularException = "Never";
System.out.println(regularException);
Pattern p = Pattern.compile(regularException);
Matcher m = p.matcher(testString);
int i = 0;
while (true) {//这么改了改方便调试
i++;//bp1
if(m.find()){
System.out.println("Match \"" + m.group() + "\" at positions "
+ m.start() + "-" + (m.end() - 1));//bp2
System.out.println(m.matches());
if (i > 200) {//bp3
break;
}
}else{
break;
}
}