Advertisement

01.12.2006 at 03:41PM PST, ID: 21694797
[x]
Attachment Details

Need help with a regular expression

Asked by GroverDill in Perl Programming Language

Tags: parse, sql, expression, regular

I'm fairly useless when it comes to regular expressions, but I'm trying to write a regular expression to parse a section out of a regular, everyday SQL statement. I think this should be pretty straightforward for someone who has some comfort with regexs. Take the following statements, for instance:

SELECT * FROM stuff 1, stuff 2
SELECT * FROM stuff 1, stuff 2 ORDER BY blah
SELECT user1, u.user2 as myuser FROM usera, userb AS u, userc LEFT JOIN userd ON blah = blah2 WHERE stuff1 = stuff2 ORDER BY stuff1

What I need is a reg ex that, when fed either of those statements (or any other SELECT statement, this needs to be robust enough to work for any valid SELECT statement thrown at it) returns ONLY the text in the FROM clause. So for the above, it would return

stuff 1, stuff 2
stuff 1, stuff 2
usera, userb AS u, userc LEFT JOIN userd ON blah = blah2

The part I'm having trouble with is telling the regex that the WHERE fields and ORDER BY fields are optional. I hate regexs...Start Free Trial
[+][-]01.12.2006 at 03:59PM PST, ID: 15687499

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.12.2006 at 10:18PM PST, ID: 15689324

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.13.2006 at 03:30AM PST, ID: 15690445

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.13.2006 at 08:42AM PST, ID: 15692928

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: parse, sql, expression, regular
Sign Up Now!
Solution Provided By: punkpulse
Participating Experts: 4
Solution Grade: A
 
 
[+][-]01.13.2006 at 09:44AM PST, ID: 15693562

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.13.2006 at 10:41AM PST, ID: 15694106

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.13.2006 at 11:14AM PST, ID: 15694456

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.14.2006 at 02:35AM PST, ID: 15699507

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32