Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: need to parse firts part of SQL-query (regex question)

by martin (Friar)
on Jan 17, 2008 at 18:22 UTC ( #662932=note: print w/ replies, xml ) Need Help??


in reply to need to parse firts part of SQL-query (regex question)

How about this: Think positive and use a regex for the fields you want rather than what might or might not separate them, like this:

my $part_sql = "f1,f2, SUM(f3),CONCAT(f4,f5, f6), f7"; my @fields = $part_sql =~ m{ (?: ^ | , ) # start of string or comma ( # begin capture (?: # either: [^,()] # non-paren non-comma | # or: \( # left paren [^()]* # any amount of non-paren \) # right paren )* # as often as possible ) # end capture }gx; # take all matches print map { "$_\n" } @fields;

Of course, this simplistic approach does not handle nested parentheses.


Comment on Re: need to parse firts part of SQL-query (regex question)
Download Code
Replies are listed 'Best First'.
Re^2: need to parse firts part of SQL-query (regex question)
by bfdi533 (Friar) on Mar 27, 2008 at 21:32 UTC

    Quite an interesting solution. Of course, as you have stated, it does not handle nested parentheses. Which, of course, is something that I need to do.

    Any takers on how this can be adapted to handle nested parens?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2015-07-07 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls