Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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# 公共类MyExample { 公共静态void Main(字符串[]args) { 字符串输入=“”; //调用Regex.Match Match m=Regex.Match(输入,“/show\u name=(.*)和show\u name\u exact=true\”>(.*)_C#_Regex - Fatal编程技术网

你知道为什么这样不行吗?C# 公共类MyExample { 公共静态void Main(字符串[]args) { 字符串输入=“”; //调用Regex.Match Match m=Regex.Match(输入,“/show\u name=(.*)和show\u name\u exact=true\”>(.*)

你知道为什么这样不行吗?C# 公共类MyExample { 公共静态void Main(字符串[]args) { 字符串输入=“”; //调用Regex.Match Match m=Regex.Match(输入,“/show\u name=(.*)和show\u name\u exact=true\”>(.*),c#,regex,C#,Regex,首先正则表达式启动“/show\u name”,但目标字符串具有“/?show\u name”,因此第一组不希望第一次预期命中 这将导致整个正则表达式失败。因为show\u name前面有问号。它在输入中,但不在模式中,因此不匹配 另外,您试图匹配,我认为这是因为您试图在前端和后端使用perl风格的斜杠。其他几个回答者已经对此感到困惑。他编写的方式是,他试图通过以/开头和结尾并在末尾加上I来不区分大小写,就像在perl中那样 但是我很确定.NET正则表达式不是这样工作的,这就是问题的原因 编辑:

首先正则表达式启动“/show\u name”,但目标字符串具有“/?show\u name”,因此第一组不希望第一次预期命中


这将导致整个正则表达式失败。

因为
show\u name
前面有问号。它在输入中,但不在模式中,因此不匹配


另外,您试图匹配
,我认为这是因为您试图在前端和后端使用perl风格的斜杠。其他几个回答者已经对此感到困惑。他编写的方式是,他试图通过以/开头和结尾并在末尾加上I来不区分大小写,就像在perl中那样

但是我很确定.NET正则表达式不是这样工作的,这就是问题的原因

编辑:更具体地说,查看RegexOptions,我从MSDN中得到的一个示例如下:

public class MyExample
{

    public static void Main(String[] args)
    {


string input = "<a href=\"http://tvrss.net/search/?show_name=The+Venture+Bros&amp;show_name_exact=true\">The Venture Bros</a></p></li>";


    // Call Regex.Match
    Match m = Regex.Match(input, "/show_name=(.*?)&amp;show_name_exact=true\">(.*?)</i");

   // Check Match instance
    if (m.Success)
    {
        // Get Group value
        string key = m.Groups[1].Value;
        Console.WriteLine(key);
        // alternate-1
    }



    }
Dim rx作为新的正则表达式(“\b(?\w+)\s+(\k)\b”,RegexOptions.Compiled或RegexOptions.IgnoreCase)
这里的关键是“RegexOptions.IgnoreCase”,它将产生您使用/pattern/i尝试的效果。

尝试以下方法:

Dim rx As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
字符串输入=“

”; //调用Regex.Match
Match m=Regex.Match(输入,“show\u name=(.*)和show\u name\u exact=true\”>(.*)在您的情况下,正确的正则表达式是

string input = "<a href=\"http://tvrss.net/search/?show_name=The+Venture+Bros&amp;show_name_exact=true\">The Venture Bros</a></p></li>";

// Call Regex.Match
Match m = Regex.Match(input, "show_name=(.*?)&amp;show_name_exact=true\">(.*?)</a");

// Check Match instance
if (m.Success)
{
    // Get Group value
    string key = m.Groups[2].Value;
    Console.WriteLine(key);
    // alternate-1
}
^.*&;show\u name\u exact=true\“\>(.*)

$
regexp很复杂,但是在这里你可以找到一个很好的教程

/?show\u name=(.)和show\u name\u exact=true\“>()

但我注意到的另一件事是,你试图得到组[1]的值,但我相信你想要组[2]的值,因为将有3组,第一组是匹配,第二组是第一组


Gl;)

好的,我们来分析一下

测试数据:


原始正则表达式:
“/show\u name=(.*)和show\u name\u exact=true\”>(.*)我试过这个:string input=“show\u name=The+Venture+Bros&show\u name\u exact=true\”>Venture Bros

“…Match m=Regex.Match(输入),/show\u name=(.*)和show\u name\u exact=true\”>(.*)是的,我认为很多人会感到困惑,忘记了这一组[0]是匹配的整个字符串。换句话说,将regex修饰符从/i更改为/a,并使用匹配数组的第三个元素而不是第二个元素(“string key=m.Groups[2].Value”).关于为什么这样更好的一些解释也会增加这个答案的帮助性。不,他不是这么做的。正如我所说,很多人对正则表达式修饰符感到困惑,他们在C#中不是这样工作的。他不是“更改正则表达式修饰符”,他是在放一个文字“/a”“最后。@Harleqin:代码说明的比解释更多。我读了你的评论两遍,明白你的意思。但是代码很明显,你可以看到你需要更改的内容。
^.*&amp;show_name_exact=true\"\>(.*)</a></p></li>$