Solved

perl, valid dns name

Posted on 2010-11-24
14
404 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
  • 7
  • 3
  • 2
  • +2
14 Comments
 

Author Comment

by:rgbcof
Comment Utility
And also lower case only.
0
 
LVL 16

Expert Comment

by:santoshmotwani
Comment Utility
0
 
LVL 16

Accepted Solution

by:
jmatix earned 333 total points
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
@jmatix

Your first pattern fails rule 2.
0
 
LVL 74

Expert Comment

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

Open in new window

0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 167 total points
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 48

Expert Comment

by:Tintin
Comment Utility
0
 
LVL 16

Expert Comment

by:jmatix
Comment Utility
@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 74

Expert Comment

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

Assisted Solution

by:jmatix
jmatix earned 333 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
@jmatix

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

Expert Comment

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

Author Closing Comment

by:rgbcof
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now