Regex to match URL


I am using the following regex to match a URL:


But its a bit screwed up because it also matches "abc.php" which I dont want. and something like abc…test. I want it to match though. and as well as

It just needs a slight tweak at the end but I am not sure what. (there should be a slash after the any domain name which it is not checking for right now and it is only checking \S)

thank you for your time.

Best Solution

$search  = "#^((?#
    the scheme:
    second level domains and beyond:
    top level domains:
    the path, can be there or not:

Just cleaned up a bit. This will match only HTTP(s) addresses, and, as long as you copied all top level domains correctly from IANA, only those standardized (it will not match http://localhost) and with the http:// declared.

Finally you should end with the path part, that will always start with a /, if it is there.

However, I'd suggest to follow Cerebrus: If you're not sure about this, learn regexps in a more gentle way and use proven patterns for complicated tasks.


By the way: Your regexp will also match something.r and something.h (between |TO| and |TR| in your example). I left them out in my version, as I guess it was a typo.

On re-reading the question: Change




(there is a ? extra) to match 'URLs' without the scheme.