The documentation says:
Qualifier: $
Description: The match must occur at the end of the string or before \n
at the end of the line or string.
Pattern example: -\d{3}$
Text match example: -333
in -901-333
I expected that the qualifier $
will match end-of-string when we use RegexOptions.Singleline
and match end-of-line when we use RegexOptions.Multiline
as follows:
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var text = @"_
abc
123
do-re-me";
var pat = @"\w+$";
var re = new Regex(pat, RegexOptions.Multiline);
var ms = re.Matches(text);
var i = 0;
foreach (Match m in ms)
Console.WriteLine($"{i++}. {m}");
Console.ReadKey();
}
}
}
The above code (RegexOptions.Multiline
) resulted in:
0. me
I used both .Net framework 4.7.1 and .Net Core 2.0 with a Console App and got the same result.
I expected the result to be:
0. _
1. abc
2. 123
3. me
Note that the ^
qualifier worked as expected. Matched the beginning of the line when using RegexOptions.Multiline
and the beginning of the string when using RegexOptions.Singleline
.
Can anyone explain the behavior of the $
qualifier?