develooper Front page | perl.beginners | Postings from February 2002

RE: regexp for validating DOS 8.3 filename

Thread Previous
Jason Larson
February 12, 2002 12:53
RE: regexp for validating DOS 8.3 filename
Message ID:
> I'm sure this is simple but I'm getting more and more confused:

Don't feel bad - I stay that way! :)

> I want to validate input so that it is either:
>    a valid dos filename (we still use it)
>    up to 12 alphanumeric characters (inc spaces)
>    nothing (in which case 'none' would be entered by default
> Here's my snippet:
> #!/usr/bin/perl
> use strict;
> my $file;
> print "\nType filename [8.3] or [enter] for none: ";
> while (<STDIN>){
>         chomp;
>         # last if ((/^\S{1,8}\.\S{0,3}/)or(/^\w{1,12}/));
>         last if (/\w{1,12}/);
>         print "try again: "
> }
> print "approved";
> Testing the above I realised that (/\w{1,12}) was allowing more than
> 12 alphanumeric characters (I've commented out the first
> 'last if' to test on it's own) and I'm not sure why.

I'm still very new to PERL myself, but I'll give this a shot...
/\w{1,12}/ matches at least 1, but not more than 12 characters in a series
of word characters (letters, numbers, or underscores).  This does not mean
that the series itself cannot be more than 12 characters, only that the
match can't be more.
For example:
$str = 1234567890123456789;
$str =~ /(\w{1,12})/;
print $1;
-- would print out 123456789012 because that is what matched.  You still
have to take into account the rest of the string.

> I've been stuck on this for a considerable time so I'd really appreciate
> someone's insight into my errors.

I'm sure there is a better way to do this, but like I said, I'm still new,
so this is one way you could accomplish it:

#use strict;
my $file;
print "\nType filename [8.3] or [enter] for none: ";
while (<STDIN>){
        /[\w ]{1,12}/;
        last if ((/^\S{1,8}\.\S{0,3}/)or($' eq ""));
        print "try again: "
print "approved";

What this does is that it performs the match, similar to what you had before
(I used character classes to include the space), but this time it checks to
see if there's anything left over (leftover pieces of the string that follow
the match go into the variable "$'").  If it sees that you entered 1-12
characters (alpha/number/underscore or a space) it will then check to see if
"$'" is blank (indicating that the series was 1-12 characters).

Bear in mind that your other match may not return results you necessarily
want.  For example, based on a match of "a string of 1-8 non-whitespace
characters followed by a "." and 1-3 non-whitespace characters" you could
get filenames such as x.x, 1.1, x.xx, x.12, or 1.23, just to name a few.




The information contained in this ELECTRONIC MAIL transmission
is confidential.  It may also be privileged work product or proprietary
information. This information is intended for the exclusive use of the
addressee(s).  If you are not the intended recipient, you are hereby
notified that any use, disclosure, dissemination, distribution [other
than to the addressee(s)], copying or taking of any action because
of this information is strictly prohibited.


Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About