Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Regex from website source

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...
You can of course Trim the result. But eh... you really shouldn't be using regex to "parse" HTML.
(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?
(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/.
(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
(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.
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
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
(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.
(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
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα
(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.
(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.
(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.
Learn how to use xpath instead.

[Image: tlTStiX.png]

(Jan 5, 2018 06:40 PM)Fire 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.
(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
[Image: 8Wj7xYL.gif]
Signature Created by Gσđfαtђα


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by LouisAug 8, 2017 01:35 PM 3 183 Aug 8, 2017 01:35 PM
Last Post: SIM-3327349
[.NET] by ShoсkwaveJul 14, 2016 07:00 AM 26 2,095 Jul 14, 2016 07:00 AM
Last Post: Ninja
[.NET] by StarZNov 9, 2015 10:22 PM 40 2,301 Nov 9, 2015 10:22 PM
Last Post: Earn