Nicholas Ferreira
Nicholas Ferreira

Reputation: 45

How to parse recursive parentheses correctly?

I need to parse a string that contains some parentheses disposed recursively, but i'm having trouble with determining priority of parentheses. For exemple, I have the string

$truth = "((A^¬B)->C)";

and I need to return what is between the parentheses. I've already done it with the following regex:

preg_match_all("~\((.*?)\)~", $truth, $str);

But the problem is that it returns what is between the first "(" and the first ")", which is

(A^¬B

Instead of this, i need it to 'know' where the parentheses closes correctly, in order to return

(A^¬B)->C

How can I return this respecting the priority order? Thanks!

Upvotes: 2

Views: 265

Answers (2)

Nick
Nick

Reputation: 147166

For your sample string, something like this will recursively give you the contents of the parentheses. It works by forcing the parentheses matched to be the outermost pair by using ^[^(]* and [^)]*$ at each end of the regex.

$truth = "((A^¬B)->C)";
while (strpos($truth, '(') !== false) {
    preg_match("~^[^(]*\((.*?)\)[^)]*$~", $truth, $str);
    $truth = $str[1];
    echo "$truth\n";
}

Output

(A^¬B)->C 
A^¬B

Note however this will not correctly parse a string such as (A+B)-(C+D). If that could be your scenario, this answer might help.

Demo on 3v4l.org

Upvotes: 3

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

The main problem you have right now is the ? non-greedy bit. If you change that to just .+ greedy it will match what you want.

$truth = "((A^¬B)->C)";
preg_match('/\(.+\)/', $truth, $match);

Try it

Output

(A^¬B)->C

If you want to match the inner pair you can use a recursive subpattern:

$truth = "((A^¬B)->C)";
preg_match('/\(([^()]+|(?0))\)/', $truth, $match);

Try It online

Output

A^¬B

If you need to go further then that you can make a lexer/parser. I have some examples here:

https://github.com/ArtisticPhoenix/MISC/tree/master/Lexers

Upvotes: 3

Related Questions