Regex.Matches c#双引号

Regex.Matches c#双引号,c#,.net,regex,C#,.net,Regex,我在下面得到了这段代码,它适用于单引号。 它查找单引号之间的所有单词。 但是如何修改正则表达式以使用双引号呢 关键词来自一个表单帖子 所以 keywords='和平'这个世界“将是”然后是“一些” //匹配所有引用字段 MatchCollection col=Regex.Matches(关键字,@“(.*)”); //将组复制到字符串[]数组 字符串[]字段=新字符串[col.Count]; for(int i=0;i

我在下面得到了这段代码,它适用于单引号。 它查找单引号之间的所有单词。 但是如何修改正则表达式以使用双引号呢

关键词来自一个表单帖子

所以

keywords='和平'这个世界“将是”然后是“一些”
//匹配所有引用字段
MatchCollection col=Regex.Matches(关键字,@“(.*)”);
//将组复制到字符串[]数组
字符串[]字段=新字符串[col.Count];
for(int i=0;i
您只需将
'
替换为
\”
,然后删除文本以正确地重建它

MatchCollection col = Regex.Matches(keywords, "\\\"(.*?)\\\"");

完全相同,但使用双引号代替单引号。双引号在正则表达式模式中并不特殊。但我通常会添加一些内容,以确保我不会在一个匹配中跨越多个带引号的字符串,并适应双引号转义:

string pattern = @"""([^""]|"""")*""";
// or (same thing):
string pattern = "\"(^\"|\"\")*\"";
它转换为文本字符串

"(^"|"")*"
使用此正则表达式:

"(.*?)"

在C#中:


是否要匹配

在这种情况下,您可能希望执行以下操作:

[Test]
public void Test()
{
    string input = "peace \"this world\" would be 'and then' some";
    MatchCollection matches = Regex.Matches(input, @"(?<=([\'\""])).*?(?=\1)");
    Assert.AreEqual("this world", matches[0].Value);
    Assert.AreEqual("and then", matches[1].Value);
}
[测试]
公开无效测试()
{
string input=“和平\”这个世界\“将是”,然后是“一些”;

MatchCollection matches=Regex.matches(输入,@“(?将引号放在字符串中不起作用吗?@-strings使用“”而不是\”作为引号。
@“(.*)”
无需在Regex.prefict中转义
。如果我想在字符串中包含引号,@user713813:移动括号(以及非reedy标记)到字符串的各个端点。
"([^"]*)"
var pattern = "\"(.*?)\"";
var pattern = "\"([^\"]*)\"";
[Test]
public void Test()
{
    string input = "peace \"this world\" would be 'and then' some";
    MatchCollection matches = Regex.Matches(input, @"(?<=([\'\""])).*?(?=\1)");
    Assert.AreEqual("this world", matches[0].Value);
    Assert.AreEqual("and then", matches[1].Value);
}