Parsing SQL Statements With Regex
I was trying to parse a SQL script the other day with regular expressions to extract some values of some columns. The script looked something like this:
NSERT INTO wp_posts (ID, post_author, post_date, post_content, post_title) VALUES (5, 1, ‘2008-02-03 20:06:31′, ‘marco’’s long text’, ‘hello world’);
so I had a regular expression that would capture anything between two apostrophes
‘(?<text>[^’]+)’
The problem is escaped apostrophes in the text itself. Like in the example above marco’’s long text it would stop at the first apostrophe.
So, in short, to get around that, I did a simple ORing like this:
‘(?<text>([^’]|[’]{2})+)’
Seems very simple, but it took me a while to see the light.