c# - 在 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;
}
解决方案
尝试以下:
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);
}
推荐阅读
- python - 如何在 python 中从 facebook messenger 发送/接收消息
- javascript - 我尝试使用 emailjs,但出现此错误“需要用户 ID”。如何解决?
- google-apps-script - 如何在每天设定的时间自动从选定的单元格中删除数据
- python - 如何读取游戏中的直接输入?
- reactjs - 从变量和状态中获取旧值
- java - 在海康威视摄像机中访问 MicroSD 上的素材
- c# - C# 如何制作实时条形图(Livecharts 库)
- google-colaboratory - 为什么即使使用 Colab Pro 帐户也无法连接到 GPU 后端?
- javascript - 如何在浏览器客户端中通过 websockets 实现 mqtt?
- python - 当 x 和 P(x) 已知时如何在 Python 中计算标准偏差