Solved

Perl string filter

Posted on 2016-08-24
5
61 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
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
tel2 earned 500 total points
Comment Utility
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
Comment Utility
Thx a lot. IT works !
0
 
LVL 11

Expert Comment

by:tel2
Comment Utility
Good to hear it, versailles.  Thanks for the points.

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

Author Comment

by:versailles
Comment Utility
I used the leading - filter, Thx !
_ are not permitted in DNs.
best
0
 
LVL 11

Expert Comment

by:tel2
Comment Utility
> "I used the leading - filter, Thx !"

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

Featured Post

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.

Join & Write a Comment

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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.
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

10 Experts available now in Live!

Get 1:1 Help Now