I’m trying to parse lines of the form:
command arg1[ arg2, ... argn]
such as:
usemtl weasels
or
f 1/2/3 4/5/6 7/8/9
Here is my regex:
^(\\w+)(( \\S+)+)$
When I parse the line “usemtl weasels”, I get the following capture groups:
Match 0: 'usemtl weasels'
Match 1: 'usemtl'
Match 2: ' weasels'
Why the space before the second match group? It doesn’t show up in Rubular.
Grouping in java regex is a little strange. Group 0 gives you the complete match of your regex – this is the same in all regex implementations I know. But group n (for n >= 1) will give you the last match of the n th declared group, not the n th match found.
Your second match gives you ‘ weasels’ with a leading blank, because your pattern contains that blank. You declared your 2nd group
(( \\S+)+)and this group gives you the second match.If you apply your pattern to the string
a b c d, your group 0 will bea b c d, group 1 will bea, group 2 will beb c dand group 3 will bed, because this is the last match of your 3rd declared (inner) group( \\S+).