Java Regex捕獲組我試圖理解這個代碼塊。在第一個中,我們在表達中尋找什么?我的理解是它是任何字符(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");
}
}}
添加回答
舉報
0/150
提交
取消