Wiggins d'Anconia wrote:
Kevin Old wrote:

Hello everyone,

I have a regex in a file upload script that I'm trying to make work
with Windows upload paths.

Here's the snippet of code I'm working with (see comments for questions):

# This is what is passed to me from the upload form
my $filename = 'C:\Documents and Settings\ata\My Documents\PDF
files\Kerr Price Protection & Titles March.xls.pdf';

# Here I'm splitting it by backslashes (\) and just popping the last element
# that contains the filename
my @fn = split /\\/, $filename;
print join("*", @fn);
my $thefilename = pop @fn;
print "filename before mods: $thefilename\n";

Checkout File::Basename, it will handle windows and unix paths, and others.

Also File::Spec has various portable functions for working with path names.


# I want to replace any non-Alpha, non-Numeric characters *except*
periods with underscores
# Currently, I'm just removing all non-Alpha and non-Numeric chars
$thefilename =~ s/\W/_/g;
print "removed funny chars: $thefilename\n";

So you want to keep alpha numeric and dots.

$thefilename =~ s/[^a-zA-Z0-9.]/_/g;

tr/// might be better:

$thefilename =~ tr/a-zA-Z0-9./_/c;


# Here I'm consolidating underscores
$thefilename =~ s/__/_/g;

If you have 4 in a row you will still be left with 2 in a row, etc.

$thefilename =~ s/_+/_/g;

$thefilename =~ tr/_//s;


Or combine them both:

$thefilename =~ tr/a-zA-Z0-9./_/sc;

$ perl -le'$_ = q/ab&c$d%_e__f.j=k/; print; tr/a-zA-Z0-9./_/sc; print'
ab&c$d%_e__f.j=k
ab_c_d_e_f.j_k


John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to