Link to home
Start Free TrialLog in
Avatar of rgbcof
rgbcof

asked on

perl, valid dns name

A valid DNS name has to be (it does not have to have .com etc.)
   1. A host name (label) can start or end with a letter or a number
   2. A host name (label) MUST NOT start or end with a '-' (dash)
   3. A host name (label) MUST NOT consist of all numeric values
   4. A host name (label) can be up to 63 characters

I am new to perl.  Can you write either one or 4 separate regular expression (i prefer multiple regex)?
Avatar of rgbcof
rgbcof

ASKER

And also lower case only.
ASKER CERTIFIED SOLUTION
Avatar of Justin Mathews
Justin Mathews

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
santoshmotwani's example is fine. If you would rather it be separated, then this should be what you need:
[1] & [2]
^[a-zA-Z0-9].*[a-zA-Z0-9]$

[3]
^(?!\d+)$

[4]
^.{1,63}$

Open in new window

@jmatix

Your first pattern fails rule 2.
Correction to account for lowercase only:
[1] & [2]
^[a-z0-9].*[a-z0-9]$

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@kaufmed:

I am checking if it starts with an alpha-num. So there is no need to check separately that it does not start with dash(-).

>>  MUST NOT start or end with a '-' (dash)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@jmatix

Also, the bound on your "length" is from 1 to Infinity. The quantifier applies to EVERYTHING inside the parentheses.
NM. You caught it in the new one  :)
Avatar of rgbcof

ASKER

Gents, the solutions are great.   The Regex library is a good way too, but our systems don't have that library and we can't make the udpate to all of them.