in reply to Re: Elegant examples to parse parenthesised strings (Parse::Balanced)
in thread Elegant examples to parse parenthesised strings

Thanks to all for your responses

As it turns out I had forgotten a case which all methods are having problems solving.

For example:

my $str = "05/04/2010 13:09:45 - A - somebody - ( ( my.my id >= 1 ) an +d ( is-mother.to code intersects afunc(val1,val2,val3,val4) and ( is- +father.to code intersects bfunc(val1,val2,val3,val4) )";

Krambambuli - Nice little one liner

cdarke - I do not have access to feature in this environment and in a very quick look at cpan I could not find what module had feature in it. Sorry, I did not pursue this example.

Marshell - I liked this, good small brute force example

repellent - This is really what I was looking for but like the others it is having problems with ( is-mother.to code intersects afunc(val1,val2,val3,val4).

my $str = "05/04/2010 13:09:45 - A - somebody - ( is-mother.to code in +tersects afunc(val1,val2,val3,val4) )"; $VAR1 = [ '05/04/2010 13:09:45 - A - somebody - ', [ '(', ' is-mother.to code intersects afunc', [ '(', 'val1,val2,val3,val4', ')' ], ' ', ')' ] ]; String is balanced.

Since it seems that all are having issues with this one scenario I am thinking I will have to combine Parse::Balanced with some brute force operations.

One idea I have is to just

Unless someone can come up with a way to handle the line in it's entirety.

Result should be my $str = "05/04/2010 13:09:45 - A - somebody - ( ( my.my id >= 1 ) and ( is-mother.to code intersects afunc(val1,val2,val3,val4) and ( i +s-father.to code intersects bfunc(val1,val2,val3,val4) )"; my.my id >= 1 is-mother.to code intersects afunc(val1,val2,val3,val4) is-father.to code intersects bfunc(val1,val2,val3,val4)

Replies are listed 'Best First'.
Re^3: Elegant examples to parse parenthesised strings (Parse::Balanced)
by repellent (Priest) on May 26, 2010 at 22:44 UTC
    Add an "escape" token for those function calls:
    @tokens = ( "(", qr/(?:[ab]func\([^)]*\))/, \@tokens, ")" );

    Parsing contents in-between the function call parentheses is left as an exercise.

    By the way, your second $str is unbalanced.