Solved

How to parse SQL containing paired single quotes?

Posted on 2009-03-31
5
521 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
ID: 24035963
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
ID: 24045328
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
ID: 24045333
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
ID: 24055387
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
ID: 31597473
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spell Check in VB6 13 95
Can we place a tooltip on the actual vb6 form 5 35
Regular expressions Exclude patter 3 28
how to know if my Checkbox is True in VB6.0? 9 36
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

948 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

21 Experts available now in Live!

Get 1:1 Help Now