Reputation: 24739
Why is my match success equal to false? I have tested the below pattern and input in Regexbuddy and it is successful.
string pattern = @"(?i)(<!-- START -->)(.*?)(?i)(<!-- END -->)";
string input = @"Hello
<!-- START -->
is there anyone out there?
<!-- END -->";
Match match = Regex.Match(input, pattern, RegexOptions.Multiline);
if (match.Success) //-- FALSE!
{
string found = match.Groups[1].Value;
Console.WriteLine(found);
}
Upvotes: 1
Views: 3460
Reputation: 11182
Try this out
string pattern = @"(?is)(<!-- START -->)(.*?)(<!-- END -->)";
string input = @"Hello
<!-- START -->
is there anyone out there?
<!-- END -->";
Match match = Regex.Match(input, pattern, RegexOptions.None);
if (match.Success) //-- FALSE!
{
string found = match.Groups[1].Value;
Console.WriteLine(found);
}
using s
option forces your pattern to match .
any character including \r
and \n
.
Upvotes: 3
Reputation: 8337
Use single line option
Regex RegexObj = new Regex("(?i)(<!-- START -->)(.*?)(?i)(<!-- END -->)",
RegexOptions.Singleline);
Upvotes: 0
Reputation: 1887
From: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
The RegexOptions.Multiline
causes ^
and $
to change their meaning so they will match on any line of the input. It does not cause .
to match \n
. For that, you need to use RegexOptions.Singleline
Upvotes: 3