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 Rhinorhino 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
Joined
Jun 6, 2005
Messages
48
Location
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

Replies
2
Views
352
  • Locked
  • Question Question
Replies
4
Views
481
  • Locked
  • Question Question
Replies
1
Views
321
  • Locked
  • Question Question
Replies
5
Views
473

Part and Inventory Search

Sponsor

Back
Top