title: Java regex matching greedy data apart from optional suffix
body: <p>Given a string like</p>
  
  <pre><code>Prefix without commas, remainder with optional suffix (optional suffix)
  </code></pre>
  
  <p>what would be the best Java regex to match and extract 3 parts of the string in one pass?</p>
  
  <ol>
  <li>The prefix up to the first comma</li>
  <li>The remainder up to the left parenthesis</li>
  <li>The suffix within the parenthesis</li>
  </ol>
  
  <p>For the above example, the 3 groups (within quotes) would be</p>
  
  <ol>
  <li>"Prefix without commas"</li>
  <li>"remainder with optional suffix"</li>
  <li>" (optional suffix)"</li>
  </ol>
  
  <p>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.</p>
  
  <p>Trying something like</p>
  
  <pre><code>([^,]*),(.*)(\s*\(.*\))?
  </code></pre>
  
  <p>only yields groups 1 and 2, putting group 3 at the end of group 2.</p>