1

I've read few articles and posts but I couldn't find out a solution in removing a specific url from a text. I have tried at least 3 different expressions. It's good to know that I'm doing it programmatically (C#).

(?<!"")((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&amp;~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])(?!"")

((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)

(?<!\w?="")(((http|https|ftp|news|file)+://)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&‌​amp;~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])

The example url is below. Note that not all urls will be exactly like that. This is the worst one.

http://api.ning.com/files/tHduipNJAW29nIL5jzWbrYON5NhTKYVXmzollyMEHasDDvnq1454BrFkXFfmzj1gH7Ye3xw03C9ulNTICYAdueKg6vaIeu/PauloSkaf.png

Could someone guide me to the solution?

UPDATE

As hwnd mentioned the best pattern is

https?\S+
5
  • What exactly are you trying to do? Commented Aug 13, 2014 at 23:34
  • To replace the entire url to an empty string. Commented Aug 13, 2014 at 23:35
  • can you post a sample text that includes the url? Commented Aug 13, 2014 at 23:49
  • @Gandarez Are there any more urls like this that starts off with api.ning.com ? Commented Aug 13, 2014 at 23:50
  • @hwnd I can't say yes because I'm invoking a third party service which provides to me this data. Commented Aug 14, 2014 at 0:00

1 Answer 1

5

I'm not fully sure what is your need but if you want to remove that url from a text, you can use a regex like this:

https?:.*(?=\s)

Working demo

Check the substitution section

enter image description here

By the way, if you want to remove whatever protocol you could use this one:

\w+:\/\/.*?(?=\s)

Working demo

enter image description here

As hwnd pointed in the comment, you can improve above regex by using:

\w+:\/\/\S*
Sign up to request clarification or add additional context in comments.

6 Comments

much easier to use https?://\S+
@hwnd you are totally right, I don't know why I always forgot about this. I'll point that in the answer. You rock man, I always learn from you +1
Take a look at this url regex101.com/r/pO8qW1/5 I used your pattern to match my text.
@Gandarez you should use the last update in my answer from hwnd comment. You can look it here regex101.com/r/pO8qW1/6
@hwnd the tick is for you. Thanks for help me improving the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.