Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Elegant examples to parse parenthesised strings (Parse::Balanced)

by back-n-black (Initiate)
on May 26, 2010 at 14:36 UTC ( [id://841735]=note: print w/replies, xml ) Need Help??


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
  • Grab the portions of the line containing this unique pattern(s).
  • Remove it from the line and deal with it seperately.
  • Finish normally with whats left of the resulting line.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://841735]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found