views:

101

answers:

4

Hi,

I need a quick regexp for validating that a string is a valid .au domain name. For example:

xxx.com.au
xxx.net.au
xxx.org.au
xxx.biz.au
xxx.--.au

all of these should be considered valid.

This should be pretty simple for someone not as hopeless with regexps as I am. Any ideas?

A: 
^[\w\d-\.]+\.au$

That should work fine. You can't really do much else since what comes before the .au might add other second level domains on top of the ones that already exist.

Evan Fosmark
Why \s? Last I checked domains didn't have whitespace in them.
Amber
Also, \w allows underscores, which aren't valid in domains. \d would be redundant if using \w since \w includes digits.
Amber
\s was a typo on my part which was only on the page for about 15 seconds. Surprised anybody caught it.
Evan Fosmark
Freak timing I suppose. :) But you really do want \a instead of \w.
Amber
What regex engine are you using? Mine (Python's re module) doesn't allow \a.
Evan Fosmark
Javascript's regex engine has it as a shortcut for alphabetic characters; if the engine you're using doesn't have it, you can use a-zA-Z instead, or if it's POSIX-compliant, [:alpha:]
Amber
+3  A: 

If you want to only allow certain secondary TLDs in the .au space:

/^[a-zA-Z0-9-]+\.(?:com|net|org|biz)\.au$/

Modify the list of secondaries separated by |'s as you desire.

If you don't mind about strictly validating the secondary TLD:

^[a-zA-Z0-9-]+\.\a+\.au$

And if you want to allow more subdomains (i.e. xxxx.yyyy.com.au):

^(?:[a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.\a+\.au$

You may need to use a-zA-Z instead of \a if the particular regex engine you're using doesn't support the latter.

Amber
This fails on the last one of his examples.
Evan Fosmark
As stated in the answer, the list of secondaries should be edited to reflect whatever values are desired to be accepted.
Amber
Hi, can you please update this to not allow subdomains? I only want to allow primary domain names since this is for a domain registration site
Click Upvote
also I'm using PHP, do you know if that supports \a?
Click Upvote
Updated for no subdomains (all that was necessary was to remove the period from within the brackets). I don't believe PHP supports \a, so I'll edit it to use a-zA-Z instead.
Amber
Also, you could simplify things and make it only a-z if you use the case-insensitive version of the regex match function (adding the "i" flag).
Amber
A: 
(.*)\.([com|net|org|biz])\.au
A: 
/^(?:[a-z0-9\-]+\.){2,}au$/i

For use with preg_match() or other PCRE functions:

if(preg_match('/^(?:[a-z0-9\-]+\.){2,}au$/i', $domain))
    echo "'$domain' is a valid domain\n";
else
    echo "'$domain' is invalid\n";
too much php
What does the {2,} part do? I don't want any limits on the number of characters of the tld
Click Upvote
`{2,}` means that (?:[a-z0-9\-]+\.) needs to appear *at least twice* before the 'au' at the end.
too much php