开发者

RegEx for not matching items in a quote

So I am trying to figure out a Regular Expression and am having some issues. What I want to find (match) is all of the SQL parameters in a large script file, but NOT match items in single quotes (such as email addresses). For example:

INSERT INTO [User]
(
   [UserGuid], [CompanyGuid], [Name], [EmailAddress]
) VALUES (
   @UserGuid1, @CompanyGuid, 'Jason', 'jason@jason.com'
)

With @UserGuid1 and @CompanyGuid matching, but not @jason matching. I have been using this RegEx:

(@+[\w]+)

But it matches the email address, so I tried to do a negative look ahead/behind like this:

(?<!')[\W](@+[\w]+)[\W](?!')

but it is matching the '(' in the following example:

INSERT INTO [User] ([UserGuid]) VALUES (@UserGuid1)

Anyone have an idea what I am missing here? Something that can sa开发者_如何转开发y: "anything that is NOT in a quote set?". Also, it is safe to assume balanced quote sets.


have you try the following?

(?<=\W)(@\w+)

basically it makes sure that the captured value preceded by a non-word character, you can add look-ahead too but it's kinda redundant because + is greedy and will match until non-word anyway.

the following will insure that in INSERT INTO [User] ([UserGuid]) VALUES ('@UserGuid1') nothing is matched:

(?<![\w'])(@\w+)


(@+[^']+) should help. The [^' ,] will match anything except a single quote, space or comma. You may need to add a few more characters, but that's the general idea.


Try this:

(?<=[^\w'])(@\w+)(?!')

This specifies that each match must be preceded by a non-word character (except for single quotes), then have an @ sign and a word, and not followed by another single quote.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜