Solved

How to parse SQL containing paired single quotes?

Posted on 2009-03-31
5
520 Views
Last Modified: 2012-05-07
I need to parse strings of the form

SELECT * FROM Table WHERE x='foo', y='foo''bar', z='bar'

and have the following matches returned by regex.split:

'foo', 'foo''bar', 'bar'

I've tried regex ('.*?') to return the quote-delimited strings, but it barfs on the single-quote pair and returns

'foo', 'foo', 'bar', 'bar'

Can someone suggest a way to do this?  Thanks.
0
Comment
Question by:Extraneus
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
Yes, don't try to do it with a simple regex. This is something best relegated to traditional parsing. I have written several hundred line SQL parsers and still not approached the full syntax.

If you must, write an explicit parsing routine that handles escaped quotes, etc.

Or,  you can try using the Perl SQL::Statement module. I have used in the past with success.

http://search.cpan.org/~rehsack/SQL-Statement-1.20/lib/SQL/Statement.pm
0
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
Comment Utility
The following regex can be used to find all literals within your select string:
Pattern:   '([^']|'')*'
 

Input:     SELECT * FROM Table WHERE x='foo', y='foo''bar', z='bar'
 
 

Output on match all:
 

"'foo'"

"'foo''bar'"

"'bar'"

Open in new window

0
 
LVL 13

Expert Comment

by:numberkruncher
Comment Utility
There are no quotes on the output, so just to clarify:
Output on match all (without quotes):
 

'foo'

'foo''bar'

'bar'

Open in new window

0
 

Author Comment

by:Extraneus
Comment Utility
Hey, thanks!  Now I need to figure out how to use regex.matches and match objects, but I see that this works.  Really appreciate it.

Dim RE As Regex = New Regex("'([^']|'')*'", RegexOptions.IgnoreCase)
Dim Matches As MatchCollection = RE.Matches(mySQL)
For Each M As Match In Matches
     response.write(M.toString)
Next

I think I can figure it out from here.

I was using and processing each string in RE.Split(mySQL), which maintained the overall SQL with the new parameters processed.  Unfortunately, this doesn't work with your regex string, but I'll take it!  Thanks a lot!
0
 

Author Closing Comment

by:Extraneus
Comment Utility
Added another comment, explaining that I haven't figure out how to use it yet, because I'm not familiar with regex.matches() yet, but I'm pretty sure I can figure that out.  Thanks a lot!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now