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?
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top