I have the following regex to check to see if a URL is valid:
preg_match('/^(http(s?):\/\/)?(www\.)?+[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/i', $url);
I like to modify this part [a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})
(at least I hope it is this bold part) to be either an IP address or this highlighted part.
At the moment, the regex is pretty good for me as it finds the bad URLs correctly - though I believe this will start failing to work correctly once the new domain policy from ICANN goes live (ie. Google may want to have the url http://search.google - instead of http://google.com for search)
Anyhow, I'd like to add the ability to allow IP addresses to also be a valid URL, but I'm unsure how to factor that into the regex
If anyone could lend a hand, then that would be great!
Two points. Top level domains now seem to max out at 6 characters (museum) so we need to account for that:
In C based languages we need to escape those \
In objective C we can define a category Method on NSString:
Note that this solution completely ignores IPv6!
This regex seems to work:
At the section after the check for "http", it simply performs an OR operation, to match either a domain name, or IP. Here is the relevant excerpt:
The IP expression is somewhat long, but it makes sure that it is a valid IP (as in, not
999.999.999.999
). You can easily substitute it for another IP check.Here it is incorporated into your earlier code: