Quantcast

Jump to content


Photo

[C#] I need help with Regex


  • Please log in to reply
12 replies to this topic

#1 HiMyNameIsNick

HiMyNameIsNick
  • Shitlord

  • 1730 posts


Users Awards

Posted 10 August 2016 - 08:40 PM

I found online a simple Neopets login but now I'm trying to obtain the value between the HTML element NP.

 

Here's the code:

 

WebClient client = new WebClient();
string html = client.DownloadString("http://www.neopets.com/index.phtml"); // This will store the whole website source code as a string
var NP = Regex.Matches(html, @"<a id='npanchor' href=' / inventory.phtml'>(.+)</a>"); // This is the line that's not working
Console.WriteLine(NP); // This prints "System.Text.RegularExpressions.MatchCollections instead the NP amount

 

I have no clue what I'm doing wrong. I've already checked and the string "html" stores the proper website source code.



#2 Adam

Adam
  • Coffee God


  • 4769 posts


Users Awards

Posted 10 August 2016 - 09:06 PM

Have you tried removing the spaces before and after the / (forward slash) in your NP var line?

 

RegexBuddy pulled this from a neo page source..

<a id='npanchor' href="/inventory.phtml">$$$$$</a>

 

$'s are placeholders for the amount of NP I have on hand. Assuming I'm right with the fix, there should be no spaces before and after the /.



#3 HiMyNameIsNick

HiMyNameIsNick
  • Shitlord

  • 1730 posts


Users Awards

Posted 10 August 2016 - 09:21 PM

 

Have you tried removing the spaces before and after the / (forward slash) in your NP var line?

 

RegexBuddy pulled this from a neo page source..

<a id='npanchor' href="/inventory.phtml">$$$$$</a>

 

$'s are placeholders for the amount of NP I have on hand. Assuming I'm right with the fix, there should be no spaces before and after the /.

 

 

 

Yup :/

 

 

Now I'm trying with the HTML agility pack

HtmlWeb hw = new HtmlWeb();
HtmlDocument htmlDoc = hw.Load(@"http://www.neopets.com/index.phtml");
HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='npanchor']"); //Here, you can also do something like (".//span[@id='point_total' class='tooltip' jQuery16207621750175125325='23' oldtitle='Note: If the number is black, your points are actually a little bit negative. Don't worry, this just means you need to start subbing again.']"); to select specific spans, etc...
Console.WriteLine(node.InnerText);

It seems that I'm getting the wrong Xpath even though I'm copy/pasting the Xpath from chrome :/

 

The only website I can't make HTML agility pack works is Neopets, I don't know what the fuck is going on


Edited by HiMyNameIsNick, 11 August 2016 - 06:34 AM.


#4 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 11 August 2016 - 06:55 AM

It prints 'System.Text.RegularExpressions.MatchCollections' because that's what Regex.Matches returns as a type. You then need to access your individual matches from the MatchCollection to get the NP value, like the example here:

 

https://msdn.microso...(v=vs.110).aspx

 

Something like this should get you started:

 

            foreach (Match mcMatch in Regex.Matches(html, @"NP\: <a id='npanchor' href=""\/inventory\.phtml"">([0-9,]+)</a>"))
            {
                Console.WriteLine(mcMatch.Groups[1].Value);
            }


#5 HiMyNameIsNick

HiMyNameIsNick
  • Shitlord

  • 1730 posts


Users Awards

Posted 11 August 2016 - 09:53 AM

 

It prints 'System.Text.RegularExpressions.MatchCollections' because that's what Regex.Matches returns as a type. You then need to access your individual matches from the MatchCollection to get the NP value, like the example here:

 

https://msdn.microso...(v=vs.110).aspx

 

Something like this should get you started:

            foreach (Match mcMatch in Regex.Matches(html, @"NP\: <a id='npanchor' href=""\/inventory\.phtml"">([0-9,]+)</a>"))
            {
                Console.WriteLine(mcMatch.Groups[1].Value);
            }

 

 

Not working for me :/ I must be doing something wrong, I'm not getting any mcMatch variable value (not even nulled).



#6 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 11 August 2016 - 10:07 AM

Not working for me :/ I must be doing something wrong, I'm not getting any mcMatch variable value (not even nulled).

 

Sure you're actually logged in? If you're just using the code in your first post then you're not going to be logged in.



#7 HiMyNameIsNick

HiMyNameIsNick
  • Shitlord

  • 1730 posts


Users Awards

Posted 11 August 2016 - 10:28 AM

Sure you're actually logged in? If you're just using the code in your first post then you're not going to be logged in.

 

 

I'm pretty sure I'm logged in :S . I'm using a code I've found on Google (from another neopets cheating site).



#8 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 11 August 2016 - 10:35 AM

Double check that you actually are.



#9 HiMyNameIsNick

HiMyNameIsNick
  • Shitlord

  • 1730 posts


Users Awards

Posted 11 August 2016 - 10:38 AM

Double check that you actually are.

 

 

Now that I think of it this was the method I was checking if I I'm logged in. Maybe I should try with something else



#10 Adam

Adam
  • Coffee God


  • 4769 posts


Users Awards

Posted 11 August 2016 - 10:41 AM

Now that I think of it this was the method I was checking if I I'm logged in. Maybe I should try with something else

Remember your other topic, and the login that I sent to you? Give that a try. It's pretty simple, and even has a condition in there, that if met will give you a messagebox indicating successful login.



#11 HiMyNameIsNick

HiMyNameIsNick
  • Shitlord

  • 1730 posts


Users Awards

Posted 11 August 2016 - 10:44 AM

Remember your other topic, and the login that I sent to you? Give that a try. It's pretty simple, and even has a condition in there, that if met will give you a messagebox indicating successful login.

 

 

I can't get it to work :( I'll give it another shot



#12 Adam

Adam
  • Coffee God


  • 4769 posts


Users Awards

Posted 11 August 2016 - 10:46 AM

I can't get it to work :( I'll give it another shot

What's the issue?? You can ask for help instead of trying some random thing you found on the internet :p.



#13 HiMyNameIsNick

HiMyNameIsNick
  • Shitlord

  • 1730 posts


Users Awards

Posted 11 August 2016 - 10:56 AM

What's the issue?? You can ask for help instead of trying some random thing you found on the internet :p.

 

 

It wasn't a code issue :p I don't know how to work with Windows Forms, I'm still learning C#




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users