Solved

Perl string filter

Posted on 2016-08-24
5
72 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 12

Accepted Solution

by:
tel2 earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script 4 97
Perl Script - Remove column of data based on column value 2 50
Add additional column to .csv using Perl. 8 128
Perl string replace for refred url 9 59
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

21 Experts available now in Live!

Get 1:1 Help Now