-2

我有字符串:

new y",[["new york",0,[]],["new york times",0,[]

我想要这些字符串["",

new york
new york times

我试过这个功能:

public MatchCollection s;
...
s =  Regex.Matches(s44, "[\".*?\",");

但我收到了这个错误:ArgumentException was unhandled: prasing "[".*?"," - Unterminated [] set

你能帮我解决这个问题吗?非常感谢!

编辑:我想要的字符串没有["",

4

3 回答 3

6

您需要转义括号。此外,您需要使用括号来介绍一个组。您想要的文本比包含在该组中:

var matches = Regex.Matches(s44, "\\[\"(.*?)\",");
foreach(Match match in matches)
{
    var result = match.Groups[1].Value;
}
于 2012-09-10T08:02:50.753 回答
2

我昨天已经回答了这个问题,使用Regex.Matches(@"(?<=\["")[^""]+")

使用 @ 前缀,您可以创建字符串文字,这意味着在您的情况下,反斜杠将作为其他字符处理,您不需要转义它们。但是你需要双引号。

后面的部分已经解释过了,所以下次请不要重新发布相同的问题。

于 2012-09-10T08:09:48.893 回答
1

这就是你想要的:

Regex.Matches(s44, "(?<=\\[\").*?(?=\",)");     

输出:new york, new york times

正则表达式演示

于 2012-09-10T08:05:44.520 回答