集体捕获的预期结果? [英] Expected outcome in group capture?
问题描述
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(1));
System.out.println("Found value: " + m.group(2));
System.out.println("Found value: " + m.group(3));
}
输出
Found value: This order was placed for QT300
Found value: 0
Found value: ! OK?
虽然我期待输出为
Found value: This order was placed for QT3000! OK?
Found value: 3000
Found value: This order was placed for QT3000! OK?
我预期产量的原因是
If pattern is "(.*)" output for m.group(1) is "This order was placed for QT3000! OK?"
If pattern is "(\\d+)" output for m.group(1) is "3000"
我不知道何时提到模式为(。*)(\\\\ +)(。*)
;为什么我没有得到预期的输出?
I don't know when I mention pattern as "(.*)(\\d+)(.*)"
; why I am not getting expected output?
推荐答案
这是由于第一个(。*)
过于贪婪并尽可能地吃掉,同时仍然允许(\d +)(。*)
来匹配其余的字符串。
It is due to the first (.*)
being too greedy and eat up as much as possible, while still allowing (\d+)(.*)
to match the rest of the string.
基本上,比赛是这样的。在开头,第一个。*
将吞噬整个字符串:
Basically, the match goes like this. At the beginning, the first .*
will gobble up the whole string:
This order was placed for QT3000! OK?
^
但是,因为我们找不到 \d +
在这里,我们回溯:
However, since we can't find a match for \d+
here, we backtrack:
This order was placed for QT3000! OK?
^
This order was placed for QT3000! OK?
^
...
This order was placed for QT3000! OK?
^
在此位置, \d +
可以匹配,所以我们继续:
At this position, \d+
can be matched, so we proceed:
This order was placed for QT3000! OK?
^
和。*
将匹配字符串的其余部分。
and .*
will match the rest of the string.
这是您看到的输出的解释。
That's the explanation for the output you see.
你可以通过制作第一个(。*)
lazy 来解决这个问题:
You can fix this problem by making the first (.*)
lazy:
(.*?)(\d+)(.*)
搜索(。*?)
的匹配将以空字符串开头,并且当它回溯时,它会逐渐增加金额它吞噬的字符:
The search for match for (.*?)
will begin with empty string, and as it backtracks, it will gradually increase the amount of characters it gobbles up:
This order was placed for QT3000! OK?
^
This order was placed for QT3000! OK?
^
...
This order was placed for QT3000! OK?
^
此时, \d +
可以匹配,。*
也可以匹配,完成匹配尝试,输出将如你所愿。
At this point, \d+
can be matched, and .*
can also be matched, which finishes the matching attempt and the output will be as you expected.
这篇关于集体捕获的预期结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!