猿问

正则表达式获取\“和\”之间的字符串

由于我是正则表达式的新手,我想在这里获得帮助。

var test = "and ( [family]: \"trees \" or [family]: \" colors \" )"

我想提取家庭列表:

树木

颜色

我使用了以下模式。

Regex.Matches(test, @"[family]:\*\");

它对我不起作用,任何建议都会有所帮助。


ibeautiful
浏览 304回答 2
2回答

繁花不似锦

您可以使用Regex.Matches(filters.queryString, @"\[family]:\s*""([^""]*)""")&nbsp; &nbsp; .Cast<Match>()&nbsp; &nbsp; .Select(m => m.Groups[1].Value.Trim())&nbsp; &nbsp; .ToList();查看正则表达式演示您需要的值在第 1 组中,并且使用.Trim(),将从这些子字符串中删除前导/尾随空格。图案详情\[family]:- 一个[family]子串\s*&nbsp;- 0+ 个空白字符"&nbsp;- 双引号([^"]*)&nbsp;- 捕获组 #1:零个或多个字符,而不是&nbsp;""&nbsp;- 双引号。C# 演示:var test = "and ( [family]: \" trees \" or [family]: \" colors \" )";var result = Regex.Matches(test, @"\[family]:\s*""([^""]*)""")&nbsp; &nbsp; &nbsp; &nbsp; .Cast<Match>()&nbsp; &nbsp; &nbsp; &nbsp; .Select(m => m.Groups[1].Value.Trim())&nbsp; &nbsp; &nbsp; &nbsp; .ToList();foreach (var s in result)&nbsp; &nbsp; Console.WriteLine(s); // => trees, colors
随时随地看视频慕课网APP
我要回答