Solved

How to parse SQL containing paired single quotes?

Posted on 2009-03-31
5
524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

696 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