Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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中创建正则表达式#_C#_Regex_Asp.net Mvc - Fatal编程技术网

C# 在C中创建正则表达式#

C# 在C中创建正则表达式#,c#,regex,asp.net-mvc,C#,Regex,Asp.net Mvc,我从一个查询中得到3种字符串: **TYPE1 :**["type1"] **TYPE2 :**["type2 type2 type2"] **TYPE3 :**["type3 type3"] 我写了一个代码,只提取单词或句子,但代码给出了一个错误,请纠正我在代码中的错误 // error in regular expression Regex _regex = new Regex(@"\"\w.+\""); Match _match = _regex.Match(temp); if (_ma

我从一个查询中得到3种字符串:

**TYPE1 :**["type1"]
**TYPE2 :**["type2 type2 type2"]
**TYPE3 :**["type3 type3"]
我写了一个代码,只提取单词或句子,但代码给出了一个错误,请纠正我在代码中的错误

// error in regular expression
Regex _regex = new Regex(@"\"\w.+\"");
Match _match = _regex.Match(temp);
if (_match.Success)
{
    resourceAnalyticsModel.ResourceFieldName = _match.Value;
}

当您使用文字字符串(
@
在字符串之前)时,必须通过双引号转义引号,因此您的模式应该是:

Regex _regex = new Regex(@"""\w.+""");
或者,如果不想使用文字字符串:

Regex _regex = new Regex("\"\\w.+\"");
请尝试以下操作:

            string[] inputs = { "**[\"type1\"]", "**[\"type2 type2 type2\"]", "**[\"type3 type3\"]" };

            foreach (string input in inputs)
            {
                Regex _regex = new Regex("\"([^\"]+)");
                Match _match = _regex.Match(input);
                Console.WriteLine(_match.Groups[1].Value);
            }

抛出的是哪个错误?我得到的是“type3 type3\”,但我想要“type3 type3”现在怎么办?它是抛出错误还是得到错误的结果?准确点!当前结果:“type2 type2 type2预期结果:type2 type2 type2Code正在给出预期结果。不确定您做错了什么。