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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Metacharacters in file names 2

Status
Not open for further replies.

hacox

Programmer
Jun 4, 2002
10
US
We receive files with metacharacters in the file names that are probably coming from a Windows environment. An example would be ORDERS'UNH.dat, I am using this command to replace the metacharacters with an underscore.

$file_name=~s/(\W)/_/g;

I have to replace the ' in this case with an _ because the service I am passing it to will not accept \'. My delimia is that I don't want to convert the dot(.). Does anyone know an easy way to do this without several lines of code? Such as this:

$file_name=~ s/\ /\\\ /g;
$file_name=~ s/\'/\\\'/g;
$file_name=~ s/\"/\\\"/g;
$file_name=~ s/\(/\\\(/g;
$file_name=~ s/\)/\\\)/g;
$file_name=~ s/\&/\\\&/g;
$file_name=~ s/\*/\\\*/g;
$file_name=~ s/\|/\\\|/g;

Thanks for you help!
Hilarie
 
Code:
$file_name=~s/[^\w.]/_/g;
Everything that not a "word" char or a '.'

jaa
 
After reading your post again, i'm not clear on what you are looking for. I don't think my first post was it, though. As a replacement for the many lines that you posted you can use
Code:
$file_name=~ s/(['"()&*| ])/\\$1/g;

jaa
 


ORDERS'UNH.dat

$file_name=~ s/(['"()&*| ])/\\$1/g;

Now I am trying to understand this but I dont get what is going on in this reg expression. Can you expain it??
 
Square brackets allow you to define your own character sets. To match a digit you might use
Code:
$str =~ s/\d//;
This would match the first digit in the string and remove it. You could do the same thing with
Code:
$str =~ s/[0123456789]//;
# Or, using a shortcut
$str =~ s/[0-9]//;
Similarly
Code:
$str =~ s/\w//;
# is equivalent to
$str =~ s/[0-9A-Za-z_]//;
You can use modifiers on these just like a predifined character class, too
Code:
$str =~ s/[13579]+//g;
This would remove all numbers in the string that contain all-odd digits.

Using [tt]()[/tt] around a character class (or any other expression) tells Perl to capture the match and store the value into the special variable $1. So
Code:
$file_name=~ s/(['"()&*| ])/\\$1/g;
matches a single character of ' or " or ( or )... etc. and it captures that character and puts a backslash in front of it.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top