Given a string like
Prefix without commas, remainder with optional suffix (optional suffix)
what would be the best Java regex to match and extract 3 parts of the string in one pass?
- The prefix up to the first comma
- The remainder up to the left parenthesis
- The suffix within the parenthesis
For the above example, the 3 groups (within quotes) would be
- "Prefix without commas"
- "remainder with optional suffix"
- " (optional suffix)"
All 3 parts of the string are of variable length. The "remainder" part may contain commas and parentheses itself and the optional suffix may or may not start with space(s), followed by left parenthesis, followed by zero or more characters, followed by right parenthesis, followed by optional spaces, followed by end-of-line.
Trying something like
([^,]*),(.*)(\s*\(.*\))?
only yields groups 1 and 2, putting group 3 at the end of group 2.