Regex Expression For Url
Solution 1:
(http|https)://([\w-]+.)+[\w-]+(/[\w- ./?%&=]*)?
from: http://www.webpronews.com/validating-a-url-with-regular-expressions-2006-10
Solution 2:
Here is a regex for you that takes into consideration if the URL contains username, if it is an IP, validates general TLDs, and some more.
^((http|https|ftp)\://)?([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$
Here is how it works: regex analyzer
This has been taken from regexlib.com, and only changed so that the scheme (http, https, etc) part is optional and replaced &
with &
.
Simpler version
Here is a simpler version, which works well most of the time:
^((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?((\S+.[a-z]{2,6})|([0-9\.]+))(:[0-9]+)?(\S+)?$
Here is how it works: regex analyzer
In case you´re interested:
Group 2: The scheme (eg. http
)
Group 3: Username and password (optional)
Group 4: Either hostname or IP
Group 5: If group 4 is hostname, this group is the hostname (eg. stackoverflow.com
)
Group 6: If group 4 is an IP, this is the IP address
Group 7: Is the port (optional)
Group 8: Is the path and query, actually the rest (eg. /hello?world=foo
)
Try it out on regexpal.
Post a Comment for "Regex Expression For Url"