我試圖找到一個更小的字符串,String patternString1 = "(John) (.+?)";
,一個更大的字符串中。較小的字符串由兩組組成,即(John) (.+?)
。不過,我只是在(.+?)
之後增加一個空格而獲得完全不同的結果。爲什麼加入`後的空間(。+?)`可以徹底改變的結果
爲String patternString1 = "(John) (.+?)";
(即無空格),結果是
found: John w
found: John D
found: John W
對於String patternString1 = "(John) (.+?) ";
,(即空間),結果是
found: John writes
found: John Doe
found: John Wayne
怎麼來的空間可以作出這樣的結果有很大差異?
String text
= "John writes about this, and John Doe writes about that,"
+ " and John Wayne writes about everything.";
String patternString1 = "(John) (.+?)";
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println("found: " + matcher.group(1) + " " + matcher.group(2));
}