I've been trying myself, and searching online, to write this regular expression but without success.
I need to validate that a given URL is from a specific domain and a well-formed link (in PHP). For example:
Good Domain: example.com
So good URLs from example.com:
- http://example.com/so/this/is/good
- http://example.com/so/this/is/good/index.html
- http://www.examle.com/so/this/is/good/mice.html#anchortag
- http://anysubdomain.example.com/so/this/is/good/wow.php
- http://anysubdomain.example.com/so/this/is/good/wow.php?search=doozy
So bad URLs not from example.com:
Some notes: I don't care about "http" verus "https" but if it matters to you assume "http" always The code that will use this regex is PHP so extra points for that.
UPDATE 2010:
Gruber adds a great URL regex:
?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
See his post: An Improved Liberal, Accurate Regex Pattern for Matching URLs