Regex for port validated URLs

I need to check the url of for example web servers. Something like http: // localhost: 8080 / xyz

How to do this using regex. Sorry, newbie in regex.

+3
source share
1 answer

relevant specifications can be found in rfc 3986 and include regular syntax definitions for all possible URL components. however, for your purposes this is likely to be too general. a somewhat compressed expression that matches only http (s) URLs will be

http[s]?://(([[:alpha:][:digit:]-._~!$&'\(\)*+,;=]|%([0-9A-F]{2}))+|([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))(:[0-9]+)?(/([[:alpha:][:digit:]-._~!$&'\(\)*+,;=]|%([0-9A-F]{2}))*)+(\?([[:alpha:][:digit:]-._~!$&'\(\)*+,;=/?]|%([0-9A-F]{2}))+)?(#([[:alpha:][:digit:]-._~!$&'\(\)*+,;=/?]|%([0-9A-F]{2}))+)?

which can be simplified to

http[s]?://(([^/:\.[:space:]]+(\.[^/:\.[:space:]]+)*)|([0-9](\.[0-9]{3})))(:[0-9]+)?((/[^?#[:space:]]+)(\?[^#[:space:]]+)?(\#.+)?)?

if you can be sure of the correct syntax for the URL components.

, , . , iana.

, ,

, carsten

+7

All Articles