Adam
Adam

Reputation: 6122

ASP.NET remove URLs from string (regex)

Already checked here: C# Remove URL from String and here and here Remove URLs from text string.

I tried the regular expression from the last post:
Regex.Replace(txt, "!\b(((ht|f)tp(s?))\://)?(www.|[a-z].)[a-z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-z0-9\.\,\;\?\\'\\\\\+&%\$#\=~_\-]+))*\b!i", "")

And I also tried the regex to which is referred:
^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$

But both do not remove the urls.

I want to remove all URLs from a string using VB. These URLs are not in an a tag.

Example string:
This is https://www.example.com/an-example-url and http://www.example.com/and-some-friendlyurl?utm_campaign=test&id=4.<br/>.Themes.

Desired result:
This is and .<br/>.Themes.

Upvotes: 0

Views: 927

Answers (1)

Trevor
Trevor

Reputation: 8004

Give this Regex a try...

 (http|https):\/\/[\w\-_]+(\.[\w\-_]+)+[\w\-\.,@?^=%&amp;:\/~‌​\+#]*[\w\-\@?^=%&amp‌​;\/~\+#]

You can combine this with a Replace and remove the matches. If you do not want the matches removed you also have each section of that match you can do what you want with them.

This has been tested and can be tried here.

Upvotes: 1

Related Questions