2007
09.21

Regex to match SQL-Style String

This post is copying the style that is used by Stephen Ostermiller in his article HERE.

Motivation

I am currently creating a RichTextBox control that will format and highlight the SQL string. I notice that the current regex that I used is not match correctly to a certain combination of SQL-style string.

Consider we have the following SQL statement:

Print ‘Testing Regex’s String Matching’; Print ‘ ‘;

First Try

‘.*’

Print ‘Testing Regex’s String Matching’; Print ‘ ‘;

This is incorrect. The regex should only match those within two single-quotes, not expand to the last single-quote.

(After n-Try)

‘.*?[^]‘

Print ‘Testing Regex’s String Matching’; Print ‘ ‘;

This should be the correct one.

For those who want to practice their Regular Expression skill, try to download THIS software, it’s free and it’s really good for Regex practice.

Credit goes to Buddie for pointing out this PAGE.

UPDATE (22 Sep 2007)

As it turns out, I totally forgot that SQL-style string doesn’t use ‘ (backslash-quote) but uses ” (quote-quote). So things getting more complicated now.
So we need to change the string into something like this:

Print ‘Testing Regex”s String Matching’; Print ‘ ‘;

Another Try

‘[a-zA-Z ]*(”)*[a-zA-Z ]*’

Print ‘Testing Regex”s String Matching’; Print ‘ ‘;

But if we change the input string into

Print ‘Testing Regex”s String”s Matching’; Print ‘ ‘;

It becomes

Print ‘Testing Regex”s String’‘s Matching’; Print ‘ ‘;

Which is wrong.

Finally

‘([a-zA-Z ]*(”)*)*’

Print ‘Testing Regex”s String” Matching’; Print ‘ ‘;

This should be the correct one.

 

Possibly Related Posts

 

Incoming Search Term

sql string regex (4), sql string regular expression (4), regex extract from sql string (2), sql regex string (2), sql regex style sorting (1), sql str str REGEXP (1), sql string email regex (1), compare sql string with regular expression (1), sql where string regexp (1), string compare in sql regexp (1), sql regex str (1), regular expressions string sql (1), regex for matching string in sql (1), regex string sql (1), regex to find a string in style tag (1), regex when using sqlstring (1), regexp match sql string (1), regexp sql with strings (1), regular expression sql string (1), regular expression string sql (1), string pattern matching in dbms (1)

Advertise Here

No Comment

Add Your Comment

* Copy this password:

* Type or paste password here: