I am a beginner trying to learn how to use regular expressions in Java. By going through a few online tutorials, I wrote the following sample codes to teach myself how regex with grouping operates, but the results are somewhat unintuitive.
String pattern = "((frok |dork )*)(\\w+) is (\\d+) Foo";
System.out.println(test1.matches(pattern));
System.out.println(test1.replaceAll(pattern, "$1"));
System.out.println(test1.replaceAll(pattern, "$3"));
With test1 = frok dork dumb is 10 Foo, I get $1 as frok dork, $3 as dumb, as expected.
However, with test1 = frok dork is 10 Foo, I expected the match to fail. Instead, I get $1 as frok and $3 as dork. Why does the dork match with \\w+ here instead of ((frok | dork )*) as in earlier case?
I did search here on SO, but these posts (Java regular expression with groups, Regular expressions, groups issue, Is there a way to use a list of string parameters with a regular expression (with groups) to construct a new string?, Regular expression with variable number of groups?) do not address this issue.
