大家好,我是Java的菜鸡,目前在学到Regular Pattern的地方
想请问正规表达式的问题,程式码如下:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main( String args[] ) {
// String to be scanned to find the pattern.
String line = "This order was placed 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) );
}else {
System.out.println("NO MATCH");
}
}
}
我想请问的是Output的部分
Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT300
Found value: 0
为什么"(.*)(\\d+)(.*)";
的m.group(1)是 This order was placed for QT300
就我目前的理解在API里面查到的内容为
. Any character (may or may not match line terminators)
X* X, zero or more times
X+ X, one or more times
\d A digit: [0-9]
.是任何字符、\\d是数字
那么digit不是从3开始到300,也就是
m.group(1):This order was placed for QT
m.group(2):3000
m.group(3):! OK?
为什么跟我想的不一样,是我误会(.*)(\\d+)(.*)的意思吗?