-1

I'm looking for a Regex that will find URLs in a string but ignore pre/following characters which are not part of the URL.

for example, from the string:

example.co.uk (main site: example.com),

The Regex will find:

example.co.uk and exaple.com.

In order to find URLs within a given string, I use the Regex '#(www\.|https?://)?[a-z0-9]+\.[a-z0-9]{2,4}\S*#i'. The problem is that if I use this regex with the given string above, it finds example.co.uk and example.com) with the closing bracket at the end.

Is there any Regex that can find URLs in a string, not matter what characters it has from both sides?

Thanks!

Itay Ganor
  • 3,965
  • 3
  • 25
  • 40
  • 2
    Possible duplicate of http://stackoverflow.com/questions/206059/php-validation-regex-for-url?rq=1 – Abdul Hameed Mar 13 '17 at 11:25
  • @AbdulHameed no, It's not a duplicate. His regex requires `http://`, and all the other comments does match the URLs with the closing bracket. – Itay Ganor Mar 13 '17 at 11:35

1 Answers1

0

You may have to use a word boundary (\b) ...

(?:www\.|https?:\/\/)?[a-z0-9]+\.[a-z0-9]{2,4}\S*\b
                                                  ^

regex demo

m87
  • 4,445
  • 3
  • 16
  • 31