• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

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)?
0
rgbcof
Asked:
rgbcof
  • 7
  • 3
  • 2
  • +2
3 Solutions
 
rgbcofAuthor Commented:
And also lower case only.
0
 
Justin MathewsCommented:
This rule will take care of rule 1,2 and 4

$dns =~ /^([a-z0-9][a-z0-9\-]*){1,63}$/;

This will take care of 3.

$dns =~ /[^\d]/;
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
käµfm³d 👽Commented:
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

0
 
käµfm³d 👽Commented:
@jmatix

Your first pattern fails rule 2.
0
 
käµfm³d 👽Commented:
Correction to account for lowercase only:
[1] & [2]
^[a-z0-9].*[a-z0-9]$

Open in new window

0
 
käµfm³d 👽Commented:
Sheesh...  I must be tired. Correction to 3 as well. Repost of all:
[1] & [2]
^[a-z0-9].*[a-z0-9]$

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

[4]
^.{1,63}$

Open in new window

0
 
Justin MathewsCommented:
@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(-).

0
 
käµfm³d 👽Commented:
>>  MUST NOT start or end with a '-' (dash)
0
 
Justin MathewsCommented:
Oops! Didn't notice the end part (I should have known better):

$dns =~ /^[a-z\d][a-z\d\-]{0,61}[a-z\d]$/;
0
 
käµfm³d 👽Commented:
@jmatix

Also, the bound on your "length" is from 1 to Infinity. The quantifier applies to EVERYTHING inside the parentheses.
0
 
käµfm³d 👽Commented:
NM. You caught it in the new one  :)
0
 
rgbcofAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now