Valamas
Valamas

Reputation: 24739

Why is my match success false?

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);
}

enter image description here

Upvotes: 1

Views: 3460

Answers (3)

Cylian
Cylian

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

PraveenVenu
PraveenVenu

Reputation: 8337

Use single line option

Regex RegexObj = new Regex("(?i)(<!-- START -->)(.*?)(?i)(<!-- END -->)",
        RegexOptions.Singleline);

Upvotes: 0

Tawnos
Tawnos

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

Related Questions