I once had to write such a regex for a company I worked for. The solution was this:
- Get a list of every ccTLD and gTLD available. Your first stop should be IANA. The list from Mozilla looks great at first sight, but lacks ac.uk for example so for this it is not really usable.
- Join the list like the example below. A warning: Ordering is important! If org.uk would appear after uk then example.org.uk would match org instead of example.
Example regex:
.*([^.]+)(com|net|org|info|coop|int|co.uk|org.uk|ac.uk|uk|__and so on__)$
This worked really well and also matched weird, unofficial top-levels like de.com and friends.
The upside:
- Very fast if regex is optimally ordered
The downside of this solution is of course:
- Handwritten regex which has to be updated manually if ccTLDs change or get added. Tedious job!
- Very large regex so not very readable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…