Why doesn't $ in .NET multiline regular expressions match CRLF?

前端 未结 1 799
花落未央
花落未央 2020-12-05 00:11

I have noticed the following:

var b1 = Regex.IsMatch(\"Line1\\nLine2\", \"Line1$\", RegexOptions.Multiline);   // true
var b2 = Regex.IsMatch(\"Line1\\r\\nLi         


        
相关标签:
1条回答
  • 2020-12-05 00:51

    From MSDN:

    By default, $ matches only the end of the input string. If you specify the RegexOptions.Multiline option, it matches either the newline character (\n) or the end of the input string. It does not, however, match the carriage return/line feed character combination. To successfully match them, use the subexpression \r?$ instead of just $.

    http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx#Multiline

    So I can't say why (compatibility with regular expressions from other languages?), but at the very least it's intended.

    0 讨论(0)
提交回复
热议问题