当我尝试获取 [ " and " , (有代码)之间的所有子字符串时,如何解决此错误?



我有字符串:

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

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

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

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

var matches = Regex.Matches(s44, "\["(.*?)",");
foreach(Match match in matches)
{
    var result = match.Groups[1].Value;
}

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

使用@前缀,您可以将字符串设置为文字,这意味着在您的情况下,backlash将作为其他字符处理,您不需要对它们进行转义。但你需要把双引号加倍。

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

这就是您想要的:

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

输出:new york, new york times

Regex Demo

最新更新