Marco Pinero
asked on
Regular Expression required
Hi folks!
I have spent some days looking for a regular expression which let me get order fields in a SQL SELECT statement.
Examples:
For all these SQL sentences I wanna extract this:
I have spent some days looking for a regular expression which let me get order fields in a SQL SELECT statement.
Examples:
SELECT * FROM TABLE
ORDER BY A, B DESC, C
SELECT * FROM TABLE
ORDER
BY A,
B DESC, C
SELECT * FROM TABLE
ORDER
BY A,
B DESC, C LIMIT 100 OFFSET 4000
SELECT * FROM TABLE
ORDER
BY A,
B DESC, C
ROWS 4000 TO 4100
For all these SQL sentences I wanna extract this:
A, B DESC, C
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Provided RegEx to extract desired text from all three test inputs, presuming that the inputs are always valid SQL.
Please supply some further details like:
what results do you receive from the database.
What do you expect to have after applying the regexp?
It may be just me, but it seems that something is missing here in your question and possibly in the way you have things going.
As I see it once you have a query to SQL server, you get the results ordered as you set them in the query. Thus, in your case: order by A then desc B and then by C. Once the query is executed and the SQL has responded you have some kind of data structure according to the fields (columns) that were fetched from the TABLE.
You don't have to have a regexp to get the fields.
Usually I hate using * in queries as using it results in a potential unknown number of fetched fields.
The only case you actually need to use a Regexp is if you receive the results in a string instead of some kind of array \ list \ directory \ dataset, a regexp is irrelevant.
if you do have the results of each row as a string, then I suppose you have some sign of delimitation like , - tab and so on. In this case, a simple split which all languages has, will do most of the job.
Since its night I wish you good night. I'm off to sleep.
-=Yuval=-