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', 'firstname.lastname@example.org' )
@CompanyGuid matching, but not @jason matching. I have been using this RegEx:
But it matches the email address, so I tried to do a negative look ahead/behind like this:
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 say: "anything that is NOT in a quote set?". Also, it is safe to assume balanced quote sets.
have you try the following?
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:
(@+[^']+) 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.
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.