RegEx help


J

JMosey

I have a string of select vars from a SQL query

i.e. Select first, second, third, fourth from table...

I can get the part "first, second, third, fourth" just fine.

I then turn that into a list which is used elsewhere.

My problem is when someone does this:

Select first, second AS NumberTwo, third, mytable.fourth from table...

For the use I have for that srting I grab, the AS and . cause problems.

I can say what I want the regex to do, but I have having a terrible
time writing it.

first, I'd like one to strip out all text backwards from "AS" until the
previous comma (i.e. make ", second AS NumberTwo," just ", NumberTwo,"

secondly, I'd like one that does the same thing with the period (i.e.
make ", mytable.fourth," just ", fourth,"

Finding the "AS" and "." are in grasp, but searching/removing backwards
until the previous "," is a pain at this point.

Is this even possible? Do I need to break my string into chunks at the
comma and approach it that way?
 
Ad

Advertisements

T

Tad McClellan

I have a string of select vars from a SQL query
My problem is when


.... you try to do Real Parsing with only regular expressions.

For the use I have for that srting I grab, the AS and . cause problems.


They cause no problems at all for something that knows how to do
a proper parse of an SQL statement.

I can say what I want the regex to do,


I can say that you shouldn't be using a regex for this at all. :)

but I have having a terrible
time writing it.


Then let someone else write the hard parts *for you*, and just re-use
what they've developed!

Have you searched CPAN for modules that can Parse SQL for you?

Like:

http://search.cpan.org/~jzucker/SQL-Statement-1.14/lib/SQL/Statement.pm

http://search.cpan.org/~jzucker/SQL-Statement-1.14/lib/SQL/Parser.pm
 

Top