I am not an expert but I am trying to write .NET regex expression to exclude SQL comment lines. Unfortunately I cannot find a proper description of how to use exclusion groups to find regex matches on another regex match: (‘.*?’) .
This regex finds all text between quotes but it also includes SQL comment areas (/*..*/ and –..):
'.*?'
Everything I tried it does not work as I am expecting.
My test sample:
IF @RetValue = 'Cat'
/*SET @RetValue = 'WrongLocation
and it works here' */
------testing line
SET @Dude = 'Punto'
/* comments */
-- But it doesn't work here because inside comments!
-- and this is aren't the end
SET @RetValue = 'But this should
work here'
Correct match should return only this:
'Cat'
''Punto'
and
'But this should
work here'
I can’t find an elegant RegEx to do what you want in one step, but you can do it in two steps.
Now you can run the RegEx
'[^']*'on commentFree to extract your fields.Note that
'[^']*'is more efficient than'.*?', because it removes the need for the RegEx engine to backtrack. Now it’ll find the start quote, continue for all characters that themselves aren’t a quote, and finish with the end quote. The same technique is used above to grab the comment after--until the end of the line\n.Edit: Alternately, you can use the pattern
\/\*.*?\*\/|\-\-[^\n]*\n|'[^']*'to match all comments and quote blocks (that are not within comments), and then only use the matches that begin with'when iterating through the results, as those will be the quote blocks.