C#正则表达式查找所有双引号文本

时间:2016-12-07 10:01:37

标签: c# regex

示例输入

  

在整个政策中,“你”和“你的”这个词指的是名字   被保险人在声明中表示。单词“我们”,“我们”和“我们的”   请参阅提供此保险的公司。 “自动”。

我尝试了类似[\s](\\?")(.*?)\1[\s|\n|\t|\b|,|.]

的内容

无法识别"us""auto." Regex Tester

可能还有其他方案,例如

  

在整个政策中,“你”和“你的”这个词指的是名字   被保险人在声明中表示。单词“我们”,“我们”和“我们的”   指提供“本保险”的“公司”。 “自动”。

这是我需要处理的另一个负面测试用例,我应该只考虑有效的双引号

  

“你拥有。这包括你获得所有权的那些”汽车“   政策开始后。

在此示例中,我应该可以删除完整的"the Company "providing" this insurance"

2 个答案:

答案 0 :(得分:0)

如果你有嵌套括号,你需要像解析器一样的东西。

否则:

string input = "Throughout this policy the words \"you\" and \"your\" refer to the Named Insured shown in the Declarations. The words \"we,\" \"us\" and \"our\" refer to the Company providing this insurance. \"auto.\"";
string[] result = Regex.Matches(input, "(\".*?\")+").Cast<Match>().Select(x => x.Value).ToArray();

答案 1 :(得分:0)

检查出来

(["'])(?:(?=(\\?))\2.)*?\1