title: Regular Expression for separating strings enclosed in parentheses
body: <p>I have a <code>String</code> that contains 2 or 3 company names each enclosed in parentheses. Each company name can also contains words in parentheses. I need to separate them using regular expressions but didn't find how.</p>
  
  <p>My <code>inputStr</code>:</p>
  
  <pre><code>(Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.)) (Motorsport racing Ltd.)
  or 
  (Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.))
  </code></pre>
  
  <p>The expected result is:</p>
  
  <pre><code>str1 = Motor (Sport) (racing) Ltd.
  str2 = Motorsport racing (Ltd.)
  str3 = Motorsport racing Ltd.
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>String str1, str2, str3;
  Pattern p = Pattern.compile("\\((.*?)\\)");
  Matcher m = p.matcher(inputStr);
  int index = 0;
  while(m.find()) {
  
      String text = m.group(1);
      text = text != null &amp;&amp; StringUtils.countMatches(text, "(") != StringUtils.countMatches(text, ")") ? text + ")" : text;
  
      if (index == 0) {
          str1= text;
      } else if (index == 1) {
          str2 = text;
      } else if (index == 2) {
          str3 = text;
      }
  
      index++;
  }
  </code></pre>
  
  <p>This works great for <code>str2</code> and <code>str3</code> but not for <code>str1</code>.</p>
  
  <p>Current result:</p>
  
  <pre><code>str1 = Motor (Sport)
  str2 = Motorsport racing (Ltd.)
  str3 = Motorsport racing Ltd.
  </code></pre>