jedyobidan
jedyobidan

Reputation: 926

Match character not in nested parentheses

How do you match a character that is not in parentheses? The expression may have an arbitrary number of nested parentheses as well. In other words, I want to split ((2+3)*10)-((10+1)/2) into ((2+3)*10) and ((10+1)/2). I would like to do this with regular expressions, if possible. I need to know how to do this because I am parsing mathematical-like expressions, so if this is not the way to go, what should I be doing?

I would prefer a solution in java, but if it is in another language I can probably figure it out as well.

Upvotes: 0

Views: 118

Answers (2)

caio
caio

Reputation: 1989

You can achieve this using a PCRE library for Java.

This feature of PCRE is called RECURSIVE PATTERNS (See documentation):

$ pcretest                                                                                                                                                                                        
PCRE version 8.31 2012-07-06

  re> / (?: \( (?: [^()]++ | (?R) )* \) ) /xg 
data> ((2+3)*10)-((10+1)/2) 
 0: ((2+3)*10)
 0: ((10+1)/2)

I don't know Java, but in PHP it works in this way:

$ php -a
Interactive shell

php > preg_match_all('/ (?: \( (?: [^()]++ | (?R) )* \) ) /x', '((2+3)*10)-((10+1)/2)', $r); var_dump($r);
array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(10) "((2+3)*10)"
    [1]=>
    string(10) "((10+1)/2)"
  }
}

Upvotes: 0

Zim-Zam O'Pootertoot
Zim-Zam O'Pootertoot

Reputation: 18158

You probably want to use a recursive descent parser. Here is an article and some example code, although the Wikipedia article from the first link has some good example C code.

There are alternatives to a recursive descent parser, such as a operator-precedence parser, but my experience from undergrad is with recursive descent parsers (I haven't parsed any mathematical expressions since then). Either way, you're essentially parsing the mathematical expression in order of operator precedence.

Upvotes: 1

Related Questions