C# System.RegEx matches LF when it should not

前端 未结 2 2207
旧巷少年郎
旧巷少年郎 2020-12-11 06:52

The following returns true

Regex.IsMatch(\"FooBar\\n\", \"^([A-Z]([a-z][A-Z]?)+)$\");

so does

Regex.IsMatch(\"FooBar\\n\",          


        
相关标签:
2条回答
  • 2020-12-11 07:02

    From wikipedia:

    $ Matches the ending position of the string or the position just before a string-ending newline. In line-based tools, it matches the ending position of any line.

    So you are asking whether there is a capital letter after the start of the beginning of the string, followed by any number of times (zero or one letter), followed by the end of the string, or the position just before the newline.

    That all seems true.

    And yes, there seems to be some mismatch between different documentation sources about what is regarded as newline and how $ works or should work exactly. It always brings to mind the wisdom:

    Sometimes a man has a problem and he figures he will use a regex to solve it.
    Now the man has two problems.

    0 讨论(0)
  • 2020-12-11 07:22

    In .NET regex, the $ anchor (as in PCRE, Python, PCRE, Perl, but not JavaScript) matches the end of line, or the position before the final newline ("\n") character in the string.

    See this documentation:

    $   The match must occur at the end of the string or line, or before \n at the end of the string or line. For more information, see End of String or Line.

    No modifier can redefine this in .NET regex (in PCRE, you can use D PCRE_DOLLAR_ENDONLY modifier).

    You must be looking for \z anchor: it matches only at the very end of the string:

    \z   The match must occur at the end of the string only. For more information, see End of String Only.

    A short test in C#:

    Console.WriteLine(Regex.IsMatch("FooBar\n", @"^[A-Z]([a-z][A-Z]?)+$"));  // => True
    Console.WriteLine(Regex.IsMatch("FooBar\n", @"^[A-Z]([a-z][A-Z]?)+\z")); // => False
    
    0 讨论(0)
提交回复
热议问题