Java regex: Repeating capturing groups

That’s right. You can’t have a “variable” number of capturing groups in a Java regular expression. Your Pattern has two groups:

\((.+?)\)(?:,\((.+?)\))*
  |___|        |___|
 group 1      group 2

Each group will contain the content of the last match for that group. I.e., abc,12 will get overridden by 30,asdf,2.

Related question:

The solution is to use one expression (something like \((.+?)\)) and use matcher.find to iterate over the matches.

Leave a Comment