I currently use this regular expression to validate URLs:
^([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?([a-z0-9-.]*)\.([a-z]{2,4})(\:0*(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{1,3}|[0-9]))?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?$
It matches a fairly long list of URLs:
google.com
google.com#a1
google.com?abc=123
google.com:80
google.com:80#a1
google.com:80?abc=123
google.com:80/test
google.com:80/test#a1
google.com:80/test?abc=123
google.com:80/test?abc=123#a1
www.google.com
www.google.com#a1
www.google.com?abc=123
www.google.com:80
www.google.com:80#a1
www.google.com:80?abc=123
www.google.com:80/test
www.google.com:80/test#a1
www.google.com:80/test?abc=123
www.google.com:80/test?abc=123#a1
www.www.google.com
www.www.google.com#a1
www.www.google.com?abc=123
www.www.google.com:80
www.www.google.com:80#a1
www.www.google.com:80?abc=123
www.www.google.com:80/test
www.www.google.com:80/test#a1
www.www.google.com:80/test?abc=123
www.www.google.com:80/test?abc=123#a1
john:smith@google.com
john:smith@google.com#a1
john:smith@google.com?abc=123
john:smith@google.com:80
john:smith@google.com:80#a1
john:smith@google.com:80?abc=123
john:smith@google.com:80/test
john:smith@google.com:80/test#a1
john:smith@google.com:80/test?abc=123
john:smith@google.com:80/test?abc=123#a1
john:smith@www.google.com
john:smith@www.google.com#a1
john:smith@www.google.com?abc=123
john:smith@www.google.com:80
john:smith@www.google.com:80#a1
john:smith@www.google.com:80?abc=123
john:smith@www.google.com:80/test
john:smith@www.google.com:80/test#a1
john:smith@www.google.com:80/test?abc=123
john:smith@www.google.com:80/test?abc=123#a1
john:smith@www.www.google.com
john:smith@www.www.google.com#a1
john:smith@www.www.google.com?abc=123
john:smith@www.www.google.com:80
john:smith@www.www.google.com:80#a1
john:smith@www.www.google.com:80?abc=123
john:smith@www.www.google.com:80/test
john:smith@www.www.google.com:80/test#a1
john:smith@www.www.google.com:80/test?abc=123
john:smith@www.www.google.com:80/test?abc=123#a1
However, it does not match these URLs which, to my knowledge, are also valid:
8.8.8.8
8.8.8.8#a1
8.8.8.8?abc=123
8.8.8.8:80
8.8.8.8:80#a1
8.8.8.8:80?abc=123
8.8.8.8:80/test
8.8.8.8:80/test#a1
8.8.8.8:80/test?abc=123
8.8.8.8:80/test?abc=123#a1
john:smith@8.8.8.8
john:smith@8.8.8.8#a1
john:smith@8.8.8.8?abc=123
john:smith@8.8.8.8:80
john:smith@8.8.8.8:80#a1
john:smith@8.8.8.8:80?abc=123
john:smith@8.8.8.8:80/test
john:smith@8.8.8.8:80/test#a1
john:smith@8.8.8.8:80/test?abc=123
john:smith@8.8.8.8:80/test?abc=123#a1
For reference, I found this one for IP addresses which seems to work well:
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
How can I tie them together? Or, is there a better regex to match all the URLs here?
Demo: