0

我的正则表达式应该像这样工作:https
://regex101.com/r/dY9jI4/1 它应该只匹配昵称(personaname)。

我的 C# 代码如下所示:

string pattern = @"personaname"":\s+""([^""]+)""";
string users = webClient.DownloadString("http://pastebin.com/raw/cDHTXXD3");
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(users);
foreach (Match match in matches)
{
    Console.WriteLine(match.Value);
}

但是在 VS15 中,我的正则表达式匹配我的整个模式,所以控制台输出看起来像:

personaname": "Tom"
personaname": "Emily"

我的模式有问题吗?我该如何解决?

4

1 回答 1

1

因此,虽然实际的答案是解析这个 JSON 并获取数据,但您的问题在于 Match,您需要match.Groups[1].Value获取那个未命名的组。

string pattern = @"personaname"":\s+""(?<name>[^""]+)""";
string users = webClient.DownloadString("http://pastebin.com/raw/cDHTXXD3");
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(users);
foreach (Match match in matches)
{
    Console.WriteLine(match.Groups["name"].Value);
}
于 2016-09-24T22:01:00.320 回答