来自 html 解析的正则表达式,如何获取特定字符串?
Regex from a html parsing, how do I grab a specific string?
我正在尝试专门获取 charactername= 之后和 " > 之前的字符串。我如何使用正则表达式让我只捕获玩家姓名?
这是我目前所拥有的,但没有用。不工作,因为它实际上不打印任何东西。在 client.DownloadString 它 returns 一个像这样的字符串:
<a href="https://my.examplegame.com/charactername=Atro+Roter" >
所以,我知道它实际上是字符串,我只是卡在了正则表达式上。
using (var client = new WebClient())
{
//Example of what the string looks like on Console when I Console.WriteLine(html)
//<a href="https://my.examplegame.com/charactername=Atro+Roter" >
// I want the "Atro+Roter"
string html = client.DownloadString(worldDest + world + inOrderName);
string playerName = "https://my.examplegame.com/charactername=(.+?)\" >";
MatchCollection m1 = Regex.Matches(html, playerName);
foreach (Match m in m1)
{
Console.WriteLine(m.Groups[1].Value);
}
}
我假设您的问题是试图解析 URL。不要 - 使用 .NET 为您提供的功能:
var playerName = "https://my.examplegame.com/?charactername=NAME_HERE";
var uri = new Uri(playerName);
var queryString = HttpUtility.ParseQueryString(uri.Query);
Console.WriteLine("Name is: " + queryString["charactername"]);
这更容易阅读,而且毫无疑问性能更高。
这里的工作示例:https://dotnetfiddle.net/iJlBKW
所有正斜杠必须像这样使用反斜杠进行转义\/
string input = @"<a href=""https://my.examplegame.com/charactername=Atro+Roter"" >";
string playerName = @"https:\/\/my.examplegame.com\/charactername=(.+?)""";
Match match = Regex.Match(input, playerName);
string result = match.Groups[1].Value;
结果 = Atro+Roter
I'm trying to specifically get the string after charactername= and before " >.
因此,您只需要先行后看并使用 LINQ 将所有匹配值放入列表中:
var input = "your input string";
var rx = new Regex(@"(?<=charactername=)[^""]+(?="")";
var res = rx.Matches(input).Cast<Match>().Select(p => p.Value).ToList();
res
变量现在应该包含您所有的角色名称。
我正在尝试专门获取 charactername= 之后和 " > 之前的字符串。我如何使用正则表达式让我只捕获玩家姓名?
这是我目前所拥有的,但没有用。不工作,因为它实际上不打印任何东西。在 client.DownloadString 它 returns 一个像这样的字符串:
<a href="https://my.examplegame.com/charactername=Atro+Roter" >
所以,我知道它实际上是字符串,我只是卡在了正则表达式上。
using (var client = new WebClient())
{
//Example of what the string looks like on Console when I Console.WriteLine(html)
//<a href="https://my.examplegame.com/charactername=Atro+Roter" >
// I want the "Atro+Roter"
string html = client.DownloadString(worldDest + world + inOrderName);
string playerName = "https://my.examplegame.com/charactername=(.+?)\" >";
MatchCollection m1 = Regex.Matches(html, playerName);
foreach (Match m in m1)
{
Console.WriteLine(m.Groups[1].Value);
}
}
我假设您的问题是试图解析 URL。不要 - 使用 .NET 为您提供的功能:
var playerName = "https://my.examplegame.com/?charactername=NAME_HERE";
var uri = new Uri(playerName);
var queryString = HttpUtility.ParseQueryString(uri.Query);
Console.WriteLine("Name is: " + queryString["charactername"]);
这更容易阅读,而且毫无疑问性能更高。
这里的工作示例:https://dotnetfiddle.net/iJlBKW
所有正斜杠必须像这样使用反斜杠进行转义\/
string input = @"<a href=""https://my.examplegame.com/charactername=Atro+Roter"" >";
string playerName = @"https:\/\/my.examplegame.com\/charactername=(.+?)""";
Match match = Regex.Match(input, playerName);
string result = match.Groups[1].Value;
结果 = Atro+Roter
I'm trying to specifically get the string after charactername= and before " >.
因此,您只需要先行后看并使用 LINQ 将所有匹配值放入列表中:
var input = "your input string";
var rx = new Regex(@"(?<=charactername=)[^""]+(?="")";
var res = rx.Matches(input).Cast<Match>().Select(p => p.Value).ToList();
res
变量现在应该包含您所有的角色名称。