static private int countOpenParens ( String s , int pos ) { java . util . regex . Pattern p = java . util . regex . Pattern . compile ( "\\(" ) ; java . util . regex . Matcher m = p . matcher ( s . subSequence ( 0 , pos ) ) ; int numParens = 0 ; while ( m . find ( ) ) { // ignore parentheses inside character classes: [0-9()a-f] // which are just literals if ( isInsideCharClass ( s , m . start ( ) ) ) { continue ; } // ignore escaped parens if ( isEscapedChar ( s , m . start ( ) ) ) continue ; if ( ! isNoncapturingParen ( s , m . start ( ) ) ) { numParens ++ ; } } return numParens ; }
Counts the open - parentheses to the left of a string position excluding escaped parentheses