当我尝试获取[“和”之间的所有子串,(有代码)时,如何修复此错误?

我有字符串:

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

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

 new york new york times 

我试过这个function:

 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(@"(?<=\["")[^""]+")

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

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

这就是你想要的:

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

输出: new york, new york times

正则表达式演示