regular expression to detect dos filenames

D

dan

I have seen this as an example of how to detect dos filenames:

$string =~ m/^\S{1,8}\.\S{0,3}/;

Unfortunately it doesn't work:

my $string = 'filename.notdos';

print 'is dos' if $string =~ m/^\S{1,8}\.\S{0,3}/;
print "\n";

$string = 'filenameplus.notdos';

print 'is not dos' unless $string =~ m/^\S{1,8}\.\S{0,3}/;
print "\n";

Outputs

is dos
is not dos

Whats going on here?
 
J

Joost Diepenmaat

dan said:
I have seen this as an example of how to detect dos filenames:

$string =~ m/^\S{1,8}\.\S{0,3}/;

Unfortunately it doesn't work:

my $string = 'filename.notdos';

That regex is not anchored at the end. Basically it matches any string
that *starts* with "a dos filename". You want something like

$string =~ m/^\S{1,8}\.\S{0,3}$/;

Also note that your regex allows dots "." in all places including the
extension, and disallows spaces. I'm not quite sure if that's correct
wrt to DOS/FAT16 filenames OR your intended use.
 
D

dan

That regex is not anchored at the end. Basically it matches any string
that *starts* with "a dos filename". You want something like

$string =~ m/^\S{1,8}\.\S{0,3}$/;

Also note that your regex allows dots "." in all places including the
extension, and disallows spaces. I'm not quite sure if that's correct
wrt to DOS/FAT16 filenames OR your intended use.

Thanks!
 
T

Tim Greer

dan said:
I have seen this as an example of how to detect dos filenames:

$string =~ m/^\S{1,8}\.\S{0,3}/;

Unfortunately it doesn't work:

my $string = 'filename.notdos';

print 'is dos' if $string =~ m/^\S{1,8}\.\S{0,3}/;
print "\n";

The above starts the string ^, but doesn't end on \S{0,3}, so it
matches.  You meant if $string =~ m/^\S{1,8}\.\S{0,3}$/;
 
$string = 'filenameplus.notdos';

print 'is not dos' unless $string =~ m/^\S{1,8}\.\S{0,3}/;
print "\n";

Again, same problem, you need to end the regex with \S{0,3}$/

I'm curious, do files on your system potentially end with ^\S{1,8}\
\S{0}?  I.e., filename.  ?  If not, or if they might not be in the
format of filename.extension, you should account for that I.e.,
filename, filename., and filename.ext, are all different.
 
D

Dr.Ruud

Petr Vileta (fidokomik) schreef:
m/^[^\\\*\?\<\>\/\:\"\|\.]{0,8}(\.[^\\\*\?\<\>\/\:\"\|\.]{1,3})*$/)

What is your purpose in writing it in such an unreadable way?

And both the "{0,8}" and the "*" quantifiers are wrong.

m#^[^\\*?<>/:"|.]{1,8}(\.[^\\*?<>/:"|.]{1,3})?$#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,211
Messages
2,571,100
Members
47,695
Latest member
KayleneBee

Latest Threads

Top