简体   繁体   中英

Regex to extract query parameters fails with DateTime Strings

Suppose the folowing SQL command:

update [TABLE] set [value] = 'UserName' where key1 = :param1 and key2 = :param2

I would like to extract :param1 and :param2 from the SQL. So, I´m using the following regex to match the SQL String:

:([\w.$]+|"[^"]+"|'[^']+')

This is working fine, except when the SQL String contains a colon (:) between quotes (") or single quotes (').

Eg, I would like that the regex matcher returns me only :param1 and :param2 to the following queries as well:

 update [TABLE] set [value] = ':UserName' where key1 = :param1 and key2 = :param2
 update [TABLE] set [value] = 'User=:UserName' where key1 = :param1 and key2 = :param2
 update [TABLE] set [value] = '2015-04-26 21:59:24' where key1 = :param1 and key2 = :param2

because the values :UserName , User=:UserName and 2015-04-26 21:59:24 are between Single Quotes...

I tried to modify the regex expression, but nothing seems to work. What should I do?

/'.*?'|(:[^\b]+?\b)/g

Your results will be in group #1 (the paren match).

Demo of this regex .

EDIT: As per weirdness discussed below: /'.*?'|(:\\B+?\\b)/g

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM