Can a single regex be used to valdate urls and match all the parts, I have been working on one and what I have come up with so far is:
(?:(?P[a
Can a single regex be used to valdate urls and match all the parts
No.
strager's regex is impressive, but at the end of the day it's less readable, maintainable and reliable than just using a proper URI parser. It necessarily rejects valid URIs and accepts strings that are not URIs, because the rules of formatting URIs cannot be fully expressed in a regex.
mailto://user@unkwndesign.com
There shouldn't be a '//' in a mailto URI. You can't tell what format the remainder (post-:) of the URI is going to be until you've read the scheme; many URI schemes do not conform to the credentials@host/path format. Best to accept only specific schemes where you know how to parse their URIs.
Modified version of mingfai's regular expression:
/^((?P<scheme>https?|ftp):\/)?\/?((?P<username>.*?)(:(?P<password>.*?)|)@)?(?P<hostname>[^:\/\s]+)(?P<port>:([^\/]*))?(?P<path>(\/\w+)*\/)(?P<filename>[-\w.]+[^#?\s]*)?(?P<query>\?([^#]*))?(?P<fragment>#(.*))?$/