How to fix this error when I try to get all substr

2019-03-07 09:01发布

I have string:

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

I want these string betwent [" and ",:

new york
new york times

I tried this function:

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

but I got this error: ArgumentException was unhandled: prasing "[".*?"," - Unterminated [] set.

Can you help me solve this problem? Thank you very much!

Edit: The string I want dont have [" and ",

3条回答
放我归山
2楼-- · 2019-03-07 09:22

I have already answered this question yesterday, use Regex.Matches(@"(?<=\["")[^""]+")

Using the @ prefix, you make the string literal, which means in your case the backlashes are handled as other characters and you don't need to escape them. But you need to double the double quotes.

The lookbehind part was already explained, so please don't repost the same question next time.

查看更多
Ridiculous、
3楼-- · 2019-03-07 09:23

This is what you want:

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

output: new york, new york times

Regex Demo

查看更多
Juvenile、少年°
4楼-- · 2019-03-07 09:37

You need to escape the bracket. Additionally, you need to introduce a group by using the parenthesis. The text you want is than contained in that group:

var matches = Regex.Matches(s44, "\\[\"(.*?)\",");
foreach(Match match in matches)
{
    var result = match.Groups[1].Value;
}
查看更多
登录 后发表回答