Search code examples
regexurldetection

Detect URL in a string without any whitespace regexp


So I know the idea of catching any URL is a very difficult task, and that's not what i'm wanting to do. I'm wanting to find a piece of regex that'll catch urls in the form of

http://something.xx.yy

http://www.something.xxx

www.something.xx.yy

in a string that will contain lots of other text and no whitespace, so for example

hellopleasevisitwww.something.xxthankyou

I've tried my best to detect something like that by myself, but it's been pretty fruitless. Any help would be great. Below are some of the expressions I tried to modify in order to have these requirements met

.*\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|].*
\\b\\w*\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]\\w*\\b
\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]

Thanks for your time


Solution

  • If it really can be as simple as you're saying...

    (http://(www\\.)?|www\\.)[^.]+\\.(\\w{3}|\\w{2}\\.\\w{2})
    

    The expressions you tried all have \\b which is a word boundary and your string unfortunately does not have word boundaries.