Hack Forums
[C#] C# Regex for URL Grabbing - 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: [C#] C# Regex for URL Grabbing (/showthread.php?tid=6014428)



C# Regex for URL Grabbing - Bruce Banner - Oct 7, 2019

Hi all, I need to grab some URL's from a page source code and am trying to do so using regex. I am not very familiar with regex and have only used it a couple times. Here is the section of the code from which I am trying to grab the links from:

Code
<script type="application/ld+json">
       {"@context":"http:\/\/schema.org","@type":"ItemList","mainEntityOfPage":{"@type":"CollectionPage","@id":"https:\/\/9gag.com\/germany"},"itemListElement":[{"@type":"ListItem","position":1,"url":"https:\/\/9gag.com\/gag\/aV0qvqn\/zufalliger-diskussionsthread-sep-30-oct-6"},{"@type":"ListItem","position":2,"url":"https:\/\/9gag.com\/gag\/a3RMy45\/fang-mal-auf"},{"@type":"ListItem","position":3,"url":"https:\/\/9gag.com\/gag\/ag5Y8Nr\/vielleicht-hast-jemand-verwendung-fur-in-der-diskussionsrunde"},{"@type":"ListItem","position":4,"url":"https:\/\/9gag.com\/gag\/az1qK6z\/wie-man-jungs-dazu-bringt-in-der-schule-besser-aufzupassen"},{"@type":"ListItem","position":5,"url":"https:\/\/9gag.com\/gag\/aQ1eQY7\/wer-kennt-es-nicht"},{"@type":"ListItem","position":6,"url":"https:\/\/9gag.com\/gag\/aEgBVDM\/harry-potter-themenparks-in-deutschland"},{"@type":"ListItem","position":7,"url":"https:\/\/9gag.com\/gag\/avo7EWZ\/vier-buchstaben"},{"@type":"ListItem","position":8,"url":"https:\/\/9gag.com\/gag\/a5RnBbL\/jedes-mal"},{"@type":"ListItem","position":9,"url":"https:\/\/9gag.com\/gag\/aO0BD9y\/anruf"},{"@type":"ListItem","position":10,"url":"https:\/\/9gag.com\/gag\/aGge8ew\/ja-dann"}]}    </script>

Please note I am parsing an entire page source code(9gag.com/Germany) into a string not just the code above. I would also be fine if I was just able to grab the id in each link such as "aV0qvqn" in the first one. Stack overflow suggested this solution: https://stackoverflow.com/questions/10576686/c-sharp-regex-pattern-to-extract-urls-from-given-string-not-full-html-urls-but but it does not work. This grabs every link in the source code besides the links I need so I am guessing the "\/\/" and other characters are messing it up? Help would be much appreciated on this, thanks!


RE: C# Regex for URL Grabbing - gigajew - Oct 7, 2019

Why has nobody suggested decoding the json and getting all the values?


Using NewtonSoft.Json library available in nuget package:

[Image: LBWM9Vn.png]

Also, if you still insist on using Regular Expression to do this, check out this: https://rubular.com/r/iW6y82MjNLRV1y or this https://rubular.com/r/bfwLyXglZwUzzV


RE: C# Regex for URL Grabbing - Bruce Banner - Oct 7, 2019

(Oct 7, 2019 08:16 PM)gigajew Wrote:  Why has nobody suggested decoding the json and getting all the values?


Using NewtonSoft.Json library available in nuget package:

[Image: LBWM9Vn.png]

Also, if you still insist on using Regular Expression to do this, check out this: https://rubular.com/r/iW6y82MjNLRV1y or this https://rubular.com/r/bfwLyXglZwUzzV

I was literally just checking out this package as I saw you post this lol! Thanks for the response I am working on it now.


RE: C# Regex for URL Grabbing - Fhack - Oct 8, 2019

Use htmlagilitypack and extract all <a href /> élément !


RE: C# Regex for URL Grabbing - 0xDEAD10CC - Oct 8, 2019

(Oct 8, 2019 06:59 AM)Fhack Wrote:  Use htmlagilitypack and extract all <a href /> élément !

URL's don't have to always exist in <a> elements or "href" attributes.


RE: C# Regex for URL Grabbing - Bruce Banner - Oct 8, 2019

Got it working with gigajew's post. Closing this thread. Thanks everyone for helping out.