Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Perl string filter

Posted on 2016-08-24
5
Medium Priority
?
108 Views
Last Modified: 2016-08-24
I have a filter in a perl script which works for a decade. I am not familiar anymore with perl. I wish to update it and I do not know how to do it.

The filter reads as:   if  ($dn !~ /^[a-zA-Z0-9\-]{3,}$/). This is to make sure a domain name string is longer than two chars and only made of letters, figures and "-". I would like to add that the first character cannot be a "0" (zero).

How is the string to read for obtaing that?
Thanks a lot
Best
0
Comment
Question by:versailles
[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
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
tel2 earned 2000 total points
ID: 41769532
Hi versailles,

You can remove the '\' because the '-' does not need to be escaped.
I have removed the 'A-Z' and added a '/i' modifier for case insensitivity, to make it slightly more concise.

Here's my first attempt:
    if ($dn !~ /^[a-z1-9-][a-z0-9-]{2,}$/i)
I think there will be a more concise and less repetitive way to do this, but the I think above should do what you have requested.  It has passed the few tests that I fed it, anyway.

Would you also like to exclude strings which start with a '-'?  If so, this should do it:
    if ($dn !~ /^[a-z1-9][a-z0-9-]{2,}$/i)

And should the '_' character be allowed or not?
0
 

Author Closing Comment

by:versailles
ID: 41769567
Thx a lot. IT works !
0
 
LVL 12

Expert Comment

by:tel2
ID: 41769569
Good to hear it, versailles.  Thanks for the points.

How about my comments about '_' and leading '-'?
0
 

Author Comment

by:versailles
ID: 41769592
I used the leading - filter, Thx !
_ are not permitted in DNs.
best
0
 
LVL 12

Expert Comment

by:tel2
ID: 41769597
> "I used the leading - filter, Thx !"

Does that mean that leading '-' are permitted or not?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
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…
Six Sigma Control Plans

636 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