in reply to Removing a Metacharacter from a filename

When you tried:

$script=~s/(\W)/_/g;
you're actually replacing every none word character with an underscore. hence your filename "cos.NEW&OLD.0826.12345" would become "cos_NEW_OLD_0826_12345" which is obviously cumbersome to write, just in case. If you want everything remain the way it is, just escape the metacharacter itself.
my $script = "cos.NEW&OLD.0826.12345"; $script=~s/\&/_/; print "$script"; #outputs "cos.NEW_OLD.0826.12345"

NOTE:Regular expression are not as consternating as they may seem!, invest the time at learning them effectively and you would see it coming your way easier than the head-whirlpool I used to suffer from when I did not know nothing about regexes, Now at least I can analyse and correlate them better. See it for yourself :)


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re^2: Removing a Metacharacter from a filename
by sputnik (Initiate) on Aug 26, 2009 at 22:57 UTC

    Thanks for your reply biohisham. The one line of code I entered did not work. It didn't replace anything as I still am getting the error message. I am a little bit at a loss.

      I tested it, it gave me the same output in my previous reply, I will update it to work without an escape character since that conforms more because the ampersand is not considered one of the metacharacters in Perl.

      However, I am sure the problem can be somewhere else, just tidy this a little bit and give us your code, more descriptively, and list these error messages, I am sure someone here would get to solve it provided it that you stated your problem in the proper light.


      Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.