Hack Forums
[.NET] Regex from website source - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Regex from website source (/showthread.php?tid=5779790)

Pages: Pages 1 2


Regex from website source - Soldiercrimes - Jan 5, 2018

Code
                                   
                             

                             
                                 
                                 
                                     
                                          6.1
                                     
                                     
                                 
                             
                             
                             
                             
                             
                          </td>

i have this in a website source and im using this to capture the 6.1:
Code
                                                    Dim r As New System.Text.RegularExpressions.Regex("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([^""]*)</td>")
                                                    Dim matches As MatchCollection = r.Matches(response)

                                                    For Each match As Match In matches

                                                        captureee = match.Value.ToString()
                                                    Next

                                                    Dim capturee As String = captureee.Replace("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", "")
                                                    Dim capture As String = capturee.Replace("</td>", "")
                                                    Dim cap As String = capture.Replace("                                           ", "")
                                                    Dim finalcap As String = Regex.Replace(cap, "^\s+$[\r\n]*", "", RegexOptions.Multiline)

But this returns the 6.1 With another blank line.. and i cant use the blank line...


RE: Regex from website source - Dialog - Jan 5, 2018

You can of course Trim the result. But eh... you really shouldn't be using regex to "parse" HTML.


RE: Regex from website source - Soldiercrimes - Jan 5, 2018

(Jan 5, 2018 07:14 AM)Dialog Wrote:  You can of course Trim the result. But eh... you really shouldn't be using regex to "parse" HTML.

What else then?


RE: Regex from website source - Dialog - Jan 5, 2018

(Jan 5, 2018 08:23 AM)Soldiercrimes Wrote:  What else then?

HTML parsers sound like they're decent at parsing HTML. Consider HTML Agility Pack: http://html-agility-pack.net/.


RE: Regex from website source - Soldiercrimes - Jan 5, 2018

(Jan 5, 2018 08:26 AM)Dialog Wrote:  HTML parsers sound like they're decent at parsing HTML. Consider HTML Agility Pack: http://html-agility-pack.net/.

Hmm regex and Trim() do the job for now :p thanks dude


RE: Regex from website source - Barret - Jan 5, 2018

(Jan 5, 2018 07:14 AM)Dialog Wrote:  You can of course Trim the result. But eh... you really shouldn't be using regex to "parse" HTML.

Dialog is right, regex is for "regulair text" which html is not, regex is not self correcting or forgiven for mistakes in the markup, it is not recommended.


RE: Regex from website source - System[X] - Jan 5, 2018

Quickly done this but not tested.

Regex pattern
Code
"&nbsp;(\n+\s+)(\d\.\d)(\n+\s+)</td>"

Then use match.Value(2)

Thanks

Edit: depending on the rest of the html code you could just use (\d\.\d) but if there are more number.number sequence it wont work


RE: Regex from website source - Soldiercrimes - Jan 5, 2018

(Jan 5, 2018 02:28 PM)System[X] Wrote:  Quickly done this but not tested.

Regex pattern
Code
"&nbsp;(\n+\s+)(\d\.\d)(\n+\s+)</td>"

Then use match.Value(2)

Thanks

Edit: depending on the rest of the html code you could just use (\d\.\d) but if there are more number.number sequence it wont work

Thanks for the tip man, it's already working with the Trim function but I will keep this in mind.


RE: Regex from website source - System[X] - Jan 5, 2018

(Jan 5, 2018 03:13 PM)Soldiercrimes Wrote:  Thanks for the tip man, it's already working with the Trim function but I will keep this in mind.

Using a better regex pattern removes the need to use Trim or Split but then again Trim and Split is only an extra 2 lines of code :)

Thanks


RE: Regex from website source - Dialog - Jan 5, 2018

(Jan 5, 2018 03:35 PM)System[X] Wrote:  Using a better regex pattern removes the need to use Trim or Split but then again Trim and Split is only an extra 2 lines of code :)

Thanks

Using regex to parse HTML is for retards.


RE: Regex from website source - Ninja - Jan 5, 2018

(Jan 5, 2018 04:55 PM)Dialog Wrote:  Using regex to parse HTML is for retards.

If it is a long document and you need a single string parsed then it is not always bad to use.


RE: Regex from website source - Dialog - Jan 5, 2018

(Jan 5, 2018 05:20 PM)Ninja Wrote:  If it is a long document and you need a single string parsed then it is not always bad to use.

Yes it is always bad to use.


RE: Regex from website source - Fire - Jan 5, 2018

Learn how to use xpath instead.


RE: Regex from website source - Ninja - Jan 5, 2018

(Jan 5, 2018 06:40 PM)90210 Wrote:  Learn how to use xpath instead.

HtmlAgilityPack has XPath, and Chrome has an option where you can right click an element and select "copy as XPath" or something like that.


RE: Regex from website source - System[X] - Jan 6, 2018

(Jan 5, 2018 04:55 PM)Dialog Wrote:  Using regex to parse HTML is for retards.

How is it not good to use regex for a simple task like this just to grab "6.1" ?

Yes lets make it better by using the agility pack but over complicates things when a single regex pattern can do the job just as good.

ll it comes down to is what you prefer and we all prefer different approaches to solve tasks.

Thanks