Apart from the group index problem mentioned in the comment, there is another question:
[...] could I as well directly bind a match to a specific matching group? Like {1} is my {0} string where test would go to match.group(0)? Is that possible?
Not with Java 6. With Java 7+, you can use named capturing groups:
Pattern.compile("(?<first>.*) is my (?<second>.*) string")
You can then use .group("first")
and .group("second")
on your Matcher
instance.
Note that .group()
is equivalent to .group(0)
.