I have noticed the following:
var b1 = Regex.IsMatch("Line1
Line2", "Line1$", RegexOptions.Multiline); // true
var b2 = Regex.IsMatch("Line1
Line2", "Line1$", RegexOptions.Multiline); // false
I'm confused. The documentation of RegexOptions says:
Multiline:
Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.
Since C# and VB.NET are mainly used in the Windows world, I would guess that most files processed by .NET applications use CRLF linebreaks (
) rather than LF linebreaks (
). Still, it seems that the .NET regular expression parser does not recognize a CRLF linebreak as an end of line.
I know that I could workaround this, for example, by matching Line1
?$
, but it still strikes me as strange. Is this really the intended behaviour of the .NET regexp parser or did I miss some hidden UseWindowsLinebreaks
option?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…