0

I'm trying to specifically get the string after charactername= and before " >. How would I use regex to allow me to catch only the player name?

This is what I have so far, and it's not working. Not working as it doesn't actually print anything. On the client.DownloadString it returns a string like this:

<a href="https://my.examplegame.com/charactername=Atro+Roter" >

So, I know it actually gets string, I'm just stuck on the regex.

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);
            }
        }
1

3 Answers 3

1

I'm trying to specifically get the string after charactername= and before " >. 

So, you just need a lookbehind with lookahead and use LINQ to get all the match values into a list:

var input = "your input string";
var rx = new Regex(@"(?<=charactername=)[^""]+(?="")";
var res = rx.Matches(input).Cast<Match>().Select(p => p.Value).ToList();

The res variable should hold all your character names now.

Sign up to request clarification or add additional context in comments.

Comments

0

I assume your issue is trying to parse the URL. Don't - use what .NET gives you:

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"]);

This is much easier to read and no doubt more performant.

Working sample here: https://dotnetfiddle.net/iJlBKW

3 Comments

I'm actually not trying to parse the URL. I want to use the Name and simply save it into a database with a timestamp by reading off the HTML source code in a website. I want to do it without using an HTMLDocument and simply taking the page source and grabbing specific things, such as the player names. It's going to be used to record all people online. as the above code only grabs people who are online.
Have you considered a library like HtmlAgilityPack?
I just said I didn't want to use HTMLDocument... The code works, I just need help with the regex.
0

All forward slashes must be unescaped with back slashes like this \/

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;

Result = Atro+Roter

2 Comments

How would you get all matches from an html source code? Input: string html = client.DownloadString(worldDest + world + inOrderName);
Your code is ok. Use Regex.Matxhes and MatchCollection. Just change a pattern. Also add lazy quantifier ?. I changed pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.