RegexOptions.Multiline?

  • Thread starter Thread starter Valery
  • Start date Start date
V

Valery

The following code unexpectedly produces mismatch:

string s = "def" + System.Environment.NewLine + "ghi";
bool b = Regex.IsMatch(s, "^def$", RegexOptions.Multiline);

whereas

string s = "abc" + System.Environment.NewLine + "def";
bool b = Regex.IsMatch(s, "^def$", RegexOptions.Multiline);

produces true as expected.

What's wrong here?

Cheers, Valery
 
string s = "def" + System.Environment.NewLine + "ghi";
bool b = Regex.IsMatch(s, "^def$", RegexOptions.Multiline);

whereas

string s = "abc" + System.Environment.NewLine + "def";
bool b = Regex.IsMatch(s, "^def$", RegexOptions.Multiline);

Use:

string s = "def\nghi";

and:

string s = "abc\ndef";

then it seems to work.

I think it is a bit weird to use *nix semantics in .NET,
but that seems to be how it is.

Docs says:

<quote>
$

The match must occur at the end of the string or before \n at the end of
the line or string.
</quote>

So it is documented.

Arne
 
Back
Top