Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的比赛成功是假的?_C#_Regex - Fatal编程技术网

C# 为什么我的比赛成功是假的?

C# 为什么我的比赛成功是假的?,c#,regex,C#,Regex,为什么我的比赛成功等于失败?我已经在Regexbuddy中测试了下面的模式和输入,它是成功的 string pattern = @"(?i)(<!-- START -->)(.*?)(?i)(<!-- END -->)"; string input = @"Hello <!-- START --> is there anyone out there? <!-- END -->"; Match match = Regex.M

为什么我的比赛成功等于失败?我已经在Regexbuddy中测试了下面的模式和输入,它是成功的

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);
}
string模式=@“(?i)(*?(?)i)()”;
字符串输入=@“您好
外面有人吗?
";
Match Match=Regex.Match(输入、模式、RegexOptions.Multiline);
if(match.Success)/--FALSE!
{
找到的字符串=匹配。组[1]。值;
控制台写入线(已找到);
}

来自:

RegexOptions.Multiline
导致
^
$
更改其含义,以便在输入的任何行上匹配。它不会导致
匹配
\n
。为此,您需要使用RegexOptions.Singleline

来自:

RegexOptions.Multiline
导致
^
$
更改其含义,以便在输入的任何行上匹配。它不会导致
匹配
\n
。为此,您需要使用RegexOptions.Singleline

使用Singleline选项

Regex RegexObj = new Regex("(?i)(<!-- START -->)(.*?)(?i)(<!-- END -->)",
        RegexOptions.Singleline);
Regex RegexObj=new Regex((?)i)(*?(?)i)()”,
RegexOptions.Singleline);
使用单行选项

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

试试这个

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);
}
stringpattern=@“(?is)()(*?)()”;
字符串输入=@“您好
外面有人吗?
";
Match Match=Regex.Match(输入、模式、RegexOptions.None);
if(match.Success)/--FALSE!
{
找到的字符串=匹配。组[1]。值;
控制台写入线(已找到);
}

使用
s
选项强制模式匹配
任何字符,包括
\r
\n
尝试一下

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);
}
stringpattern=@“(?is)()(*?)()”;
字符串输入=@“您好
外面有人吗?
";
Match Match=Regex.Match(输入、模式、RegexOptions.None);
if(match.Success)/--FALSE!
{
找到的字符串=匹配。组[1]。值;
控制台写入线(已找到);
}

使用
s
选项将强制模式匹配
任何字符,包括
\r
\n

您确定.NET接受大小写忽略语法吗?第一个
(?i)
使其后面的所有字符都不区分大小写,因此第二个字符没有任何用处。如果要限制其效果,可以使用以下表单:
(?i:)
。但是,在这种情况下没有必要,因为它只影响
开始
结束
。您确定.NET接受大小写忽略语法吗?第一个
(?i)
使后面的所有内容不区分大小写,因此第二个没有任何用处。如果要限制其效果,可以使用以下表单:
(?i:)
。不过,在这种情况下没有必要,因为它只影响
START
END