首页 > 解决方案 > 在 C# 中创建正则表达式

问题描述

我从查询中得到 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;
}

标签: c#regexasp.net-mvc

解决方案


尝试以下:

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

推荐阅读