title: Extracting Operation(...); and sub Operation from String using REGEX
body: <p>I have an issue with a Regex in java for Android.
  i would like to retreive the first operation (and each sub operations) like in the following samples:</p>
  
  <ol>
  <li>"OPERATION(ASYNC_OPERATION,_RFID_ITEM_SERIAL);"</li>
  <li>"OPERATION(CONCAT,~1261,01,OPERATION(ASYNC_OPERATION,_RFID_ITEM_ID);,21,OPERATION(ASYNC_OPERATION,_RFID_ITEM_SERIAL););"</li>
  </ol>
  
  <p>As you can see each Operation can have sub Operations... And that's where i'm getting problems.</p>
  
  <p>Actually i am using this Regex: <code>^\s*(OPERATION\s*\(\s*)(.*)(\);)</code></p>
  
  <p>but the index of ");" returned is always the last index, and in case of two sub operations, inside of a "Main" operation, this is wrong...</p>
  
  <pre><code>private static Pattern operationPattern=Pattern.compile("^\\s*(OPERATION\\s*\\(\\s*)(.*)(\\);)",Pattern.CASE_INSENSITIVE);
  
      public Operation(String text){
          parseOperationText(text);
      }
  
      private void parseOperationText(String text){
          String strText = text.replace("#,", "§");
          Matcher matcher=operationPattern.matcher(strText);
          if(matcher.find()) {
              //This is an OPERATION
              subOperations=new ArrayList&lt;&gt;();
              String strChain = matcher.group(2);//Should only contain the text between "OPERATION(" and ");"
  
              int commaIdx = strChain.indexOf(",");
              if (commaIdx == -1) {
                  //Operation without parameter
                  operationType = strChain;
              } else {
                  //Operation with parameters
                  operationType = strChain.substring(0, commaIdx);
                  strChain = strChain.substring(commaIdx + 1);
                  while (strChain.length()&gt;0) {
                      matcher = operationPattern.matcher(strChain);
                      if (matcher.find()) {
                          String subOpText=matcher.group(0);
                          strChain=StringUtils.stripStart(strChain.substring(matcher.end())," ");
                          if(strChain.startsWith(",")){
                              strChain=strChain.substring(1);
                          }
                          subOperations.add(new Operation(subOpText));
                      }
                      else{
                          commaIdx = strChain.indexOf(",");
                          if(commaIdx==-1)
                          {
                              subOperations.add(new Operation(strChain));
                              strChain="";
                          }
                          else{
                              subOperations.add(new Operation(strChain.substring(0,commaIdx)));
                              strChain=strChain.substring(commaIdx+1);
                          }
                      }
                  }
              }
          }
          else {
              //Not an operation
              //...
          }
      }
  </code></pre>
  
  <p>It works for sample 1 but for Sample 2, after finding the "Main" operation (CONCAT in the sample), the second match returns this:</p>
  
  <p>OPERATION(ASYNC_OPERATION,_RFID_ITEM_ID);,21,OPERATION(ASYNC_OPERATION,_RFID_ITEM_SERIAL);</p>
  
  <p>What i would like to retrieve is this:</p>
  
  <ol>
  <li>"CONCAT,~1261,01,OPERATION(ASYNC_OPERATION,_RFID_ITEM_ID);,21,OPERATION(ASYNC_OPERATION,_RFID_ITEM_SERIAL);"</li>
  <li>"ASYNC_OPERATION,_RFID_ITEM_ID"</li>
  <li>"ASYNC_OPERATION,_RFID_ITEM_SERIAL"</li>
  </ol>