Solved

Perl string filter

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filtering a file to table 9 102
perl to display a matrix report for 3-dimenional array 3 73
stftime format 4 58
Add additional column to .csv using Perl. 8 165
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…
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…

730 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