Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to parse SQL containing paired single quotes?

Posted on 2009-03-31
5
Medium Priority
?
529 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 2000 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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

604 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