我正在试图理解这个代码块。在第一个问题中,我们要从表达式中寻找什么?
我的理解是,它是任何字符(0或多次*),后跟0到9之间的任何数字(一次或多次+),后跟任何字符(0或多次*)。
当执行此命令时,结果是:
Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT300
Found value: 0
谁能跟我说一遍?
使用捕获组的优势是什么?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTut3 {
public static void main(String args[]) {
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");
}
}
}