Solved

perl, valid dns name

Posted on 2010-11-24
14
423 Views
Last Modified: 2012-05-10
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
Comment
Question by:rgbcof
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +2
14 Comments
 

Author Comment

by:rgbcof
ID: 34209360
And also lower case only.
0
 
LVL 16

Accepted Solution

by:
jmatix earned 333 total points
ID: 34209500
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34209516
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34209524
@jmatix

Your first pattern fails rule 2.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34209541
Correction to account for lowercase only:
[1] & [2]
^[a-z0-9].*[a-z0-9]$

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 167 total points
ID: 34209565
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
 
LVL 48

Expert Comment

by:Tintin
ID: 34209583
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34209692
@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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34209712
>>  MUST NOT start or end with a '-' (dash)
0
 
LVL 16

Assisted Solution

by:jmatix
jmatix earned 333 total points
ID: 34209797
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34209802
@jmatix

Also, the bound on your "length" is from 1 to Infinity. The quantifier applies to EVERYTHING inside the parentheses.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34209806
NM. You caught it in the new one  :)
0
 

Author Closing Comment

by:rgbcof
ID: 34210049
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Six Sigma Control Plans

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question