• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 116
  • Last Modified:

Perl string filter

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
versailles
Asked:
versailles
  • 3
  • 2
1 Solution
 
tel2Commented:
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
 
versailleschercheur libreAuthor Commented:
Thx a lot. IT works !
0
 
tel2Commented:
Good to hear it, versailles.  Thanks for the points.

How about my comments about '_' and leading '-'?
0
 
versailleschercheur libreAuthor Commented:
I used the leading - filter, Thx !
_ are not permitted in DNs.
best
0
 
tel2Commented:
> "I used the leading - filter, Thx !"

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now