2

I need to clean all links in long string. Eg:

http://www.example.com

http://example.com

www.example.net

youtube.com/user/123456

facebook.com/example

sub.domain.com

I want remove all link and choosing this code but not working all links.

(?>http?://|ww\w\.).+?(?=\s)

How to fix this problem?

Thank you!

2 Answers 2

3

Regular Expression:

(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)

Example Execution: http://regexr.com?38bj4

Reference: http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149

2
  • This is quite robust! Was surprised at how many different cases it caught, thank you!
    – wtk219
    Commented Jul 19, 2023 at 14:05
  • fails, if a link contains a port-definition (i.e. https://domain.tld:8443/
    – dognose
    Commented Sep 25, 2023 at 11:43
1

Same as Matthew wrote but would get rid of that space in the last bracket - then it won't catch the text after the link. So it would be like this:

(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)

PS. Sorry Matthew - I cannot yet comment. But I've created the account to thank you for your reply - it solved my issue as well :)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.