I'm trying to come up with a regex pattern that will match any domains in this format:
but not this:
Currently it needs to only cover the main TLDs (com, net, org), but I'd like it to be able to handle others (like co.uk, com.br, etc.) for flexibility.
So far I've got this, but it definitely needs some work:
Could a regex ninja help me out?
EDIT: The regex will be used in PHP, and there is never a protocol on the beginning of the string to match due to the setup of the script. I'd have to dig more into the script to get more details on why this is true, but I believe it is just grabbing the host name from the PHP $_SERVER variable.
Perhaps this would work to cover anything but a period up to something matching .xyz or .xyz.ab or .xyz.abc
I've got the nearly completed pattern:
updated below (php requires / and / at the beginning and end)
Can anyone poke holes in the implementation? It appears to be working as expected.
This is where I'm currently at:
It matches nearly what I want, though it requires the / at the beginning of the filepath so example.com does not match, while example.com/test does. I can't get it to match example.com without matching the ".exa" in "www.example.com".
Ok, we've got a winner:
www.123.com (matches all URLs with domains shorter than 4 characters)