开发者

Why doesn't $ always match to an end of line

Below is a simple code snippet that demonstrates the seemingly buggy behavior of end of line matching ("$") in .Net regular expressions. Am I missing something obvious?

        string input = "Hello\nWorld\n";
        string regex = @"^Hello\n^World\n";  //Match
        //regex = @"^Hello\nWorld\n";  //Match
        //regex = @"^Hello$";  //Match
        //regex = @"^Hello$World$";  //No match!!!
        //regex = @"^Hello$^World$";  //No match!!!

        Match m = Regex.Match(input, regex, RegexOptions.Multiline开发者_JAVA百科 | RegexOptions.CultureInvariant);
        Console.WriteLine(m.Success);


$ does not consume the newline character(s). @"^Hello$\s+^World$" should match.


The $ doesn't match a newline. It matches the end of the string in which the pattern is applied (unless multiline mode is enabled). There isn't much sense in having two ends in a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜