I am trying to write out a regex to get each insert line from a SQL script. When I use the .NET Regex Tester on Regex Hero I get my expected 8 matches. However, when I run this
Note that toggling the "CrLf marks a line ending" setting on the Regex Hero page causes the 8 lines to stop being matched; this is a clue as to what's causing the problem.
In your C# code, the line breaks within the literal string are encoded as a CR/LF pair ("\r\n"
). The $
in the regex (that matches an end-of-line in Multiline mode) only matches the \n
character. Thus, there is an extra \r
character between the final comma (or semicolon) which the regex doesn't account for, and the match fails.
Some ways you could address this problem include:
text = text.Replace("\r\n", "\n");
, orstring query = @"^\(.*?\)(,|;)\r$";