这是因为的贪婪,
*随之而来的是回溯。
字串:
This order was placed for QT3000! OK?
正则表达式:
(.*)(\d+)(.*)
我们都知道那
.*是贪婪的,并且尽可能匹配所有字符。因此,第一个
.*匹配所有字符,直到最后一个匹配
?,然后按顺序回溯以提供匹配。正则表达式中的下一个模式是
d+,因此它回溯到一个数字。一旦找到数字,就
d+匹配该数字,因为这里满足条件(
d+匹配一个或多个数字)。现在,第一个
(.*)捕获
This order was placed forQT300,随后的
(\d+)捕获捕获
0位于
!符号之前的数字。
现在,下一个模式将
(.*)捕获剩余的所有字符
!<space>OK?。
m.group(1)指的是出现在组索引1内的字符,并
m.group(2)指代索引2,就像它继续一样。
在这里查看演示。
获得所需的输出。
String line = "This order was placed for QT3000! OK?"; String pattern = "(.*)(\d{2})(.*)"; // Create a Pattern object Pattern r = Pattern.compile(pattern); // Now create matcher object. Matcher m = r.matcher(line); while(m.find( )) { System.out.println("Found value: " + m.group(1)); System.out.println("Found value: " + m.group(2)); System.out.println("Found value: " + m.group(3)); }
输出:
Found value: This order was placed for QT30Found value: 00Found value: ! OK?
(.*)(\d{2}),按顺序回溯最多两位数以提供匹配。
改变你的模式,
String pattern = "(.*?)(\d+)(.*)";
要获得类似的输出,
Found value: This order was placed for QTFound value: 3000Found value: ! OK?
?在
*部队
*进行非贪婪的比赛之后。
使用额外的捕获组从单个程序获取输出。
String line = "This order was placed for QT3000! OK?";String pattern = "((.*?)(\d{2}))(?:(\d{2})(.*))";Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(line); while(m.find( )) { System.out.println("Found value: " + m.group(1)); System.out.println("Found value: " + m.group(4)); System.out.println("Found value: " + m.group(5)); System.out.println("Found value: " + m.group(2)); System.out.println("Found value: " + m.group(3) + m.group(4)); System.out.println("Found value: " + m.group(5)); }
输出:
Found value: This order was placed for QT30Found value: 00Found value: ! OK?Found value: This order was placed for QTFound value: 3000Found value: ! OK?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)