Archive for the 'Regular Expressions' Category

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.

Regex for U.S and Canadian Zip Codes

for my future references, below is a regular expression for validating U.S and Canadian zip codes.

^\d{5}(?:-\d{4})?$|^[a-zA-Z]\d[a-zA-Z]\s?\d[a-zA-Z]\d$

more details on the format of Canadian zip codes