我试图理解此代码块。在第一个中,我们在表达式中寻找什么?
我的理解是,它是任意字符(0或多次*),后跟0到9之间的任意数字(1或多次+),后跟任意字符(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");
}
}
}
1
要插入新行,请在行末放置2个空格。有关降价语法的更多信息:en.wikipedia.org/wiki/Markdown-另请参见:stackoverflow.com/editing-help
—
assylias 2013年