我在线学习Java正则表达式教程,并对一个小程序感到困惑。关于查找数字字符串的Java正则表达式
// String to be scanned to find the pattern.
String line = "This order was places for QT3000! OK?";
String pattern = "(.*)(\\d+)(.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Found value: " + m.group(0));
System.out.println("Found value: " + m.group(1));
System.out.println("Found value: " + m.group(2));
}
并打印出来的结果是:
Found value: This order was places for QT3000! OK?
Found value: This order was places for QT300
Found value: 0
我不知道为什么组(1)获取价值以上的价值?为什么它会在'QT3000'的最后一个零之前停止?
非常感谢!
你在组1人失踪零是正确的,在组2 ... 3组将是'! OK?'。 – cha0site 2012-07-11 14:33:50
Java正则表达式..这听起来很可怕 – gaussblurinc 2012-07-11 14:40:01