Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for valid subdomain in Ruby

I'm attempting to validate a string of user input that will be used as a subdomain. The rules are as follows:

  1. Between 1 and 63 characters in length (I take 63 from the number of characters Google Chrome appears to allow in a subdomain, not sure if it's actually a server directive. If you have better advice on valid max length, I'm interested in hearing it)
  2. May contain a-zA-Z0-9, hyphen, underscore
  3. May not begin or end with a hyphen or underscore

EDIT: From input below, I've added the following: 4. Should not contain consecutive hyphens or underscores.

Examples:

a => valid
0 => valid
- => not valid
_ => not valid
a- => not valid
-a => not valid
a_ => not valid
_a => not valid
aa => valid
aaa => valid
a-a-a => valid
0-a => valid
a&a => not valid
a-_0 => not valid
a--a => not valid
aaa- => not valid

My issue is I'm not sure how to specify with a RegEx that the string is allowed to be only one character, while also specifying that it may not begin or end with a hyphen or underscore.

Thanks!

like image 935
gsr Avatar asked Mar 04 '11 16:03

gsr


1 Answers

You can't can have underscores in proper subdomains, but do you need them? After trimming your input, do a simple string length check, then test with this:

/^[a-z\d]+(-[a-z\d]+)*$/i

With the above, you won't get consecutive - characters, e.g. a-bbb-ccc passes and a--d fails.

/^[a-z\d]+([-_][a-z\d]+)*$/i

Will allow non-consecutive underscores as well.


Update: you'll find that, in practice, underscores are disallowed and all subdomains must start with a letter. The solution above does not allow internationalised subdomains (punycode). You're better of using this

/\A([a-z][a-z\d]*(-[a-z\d]+)*|xn--[\-a-z\d]+)\z/i
like image 104
Walf Avatar answered Sep 22 '22 23:09

Walf