C# System.RegEx matches LF when it should not
The following returns true
Regex.IsMatch("FooBar\n", "^([A-Z]([a-z][A-Z]?)+)$");
so does
Regex.IsMatch("FooBar\n", "^[A-Z]([a-z][A-Z]?)+$");
The RegEx is in SingleLine mode by default, so $ should not match \n. \n is not an allowed character.
This is to match a single ASCII PascalCaseWord (yes, it will match a trailing Cap)
Doesn't work with any combinations of RegexOptions.Multiline | RegexOptions.Singleline
What am I doing wrong?
Solution 1:
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
Solution 2:
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.