Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGEX Filename Validation

Status
Not open for further replies.

LocoPollo

Programmer
Jun 6, 2005
48
DK
Does anyone have a regex line that either removes illegal characters or validates a string so that it can be used as file- and foldernames on linux and windows??

Its probably really simple, but I have spend a day trying to figure it out.

Thanks!
 
the convention is to only use certain characters for names:

a-z A-Z _ . -

so a regexp might look like this:

Code:
unless ($file =~ m/^[\w.-]+$/) {
    do something
}

\w is a character class for a-zA-Z_

and if you are using taint checking capture the pattern in memory to use to untaint the name:

Code:
unless ($file =~ m/^([\w.-]+)$/) {
    die "Illegal file name: $file";
}
my $untainted = $1;
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top