output:#!/usr/bin/perl -w use strict; sub fixn($) { my $str = "@_"; $str =~ s/[^a-zA-Z0-9^_\-\\`\.\[\]\{\}~]//g; return $str; } sub fixn2 { # no prototype needed my $str = shift; # since we only ever want 1 argument # which will be converted to a string # if needed anyway. $str =~ s/[^a-zA-Z0-9^_\-\\`\.\[\]\{\}~]//g; return $str; } my $input = 'a b \\ c d'; print "input='$input'\n"; my $myvar = fixn($input); print "myvar='$myvar'\n"; my $better = fixn2($input); print "better='$better'\n";
input='a b \ c d' myvar='ab\cd' better='ab\cd'
In other words, your regex is fine, but your code is sloppy, and your problem is probably somewhere in do_whatever_I_want_with()
update:
I have the feeling, that backslashes are lost in my $str = "@_"; <= that quotes turn them into a escape sequences.
That doesn't happen: backslashes are only interpreted in literal strings, they are not "recursively" interpreted in interpolated variables. The only exception is when you embed a variable in a regex, like so:
$var = "bla[abc]\\t"; # $var contains bla[abc]\t /$var/; # is equivalent to /bla[abc]\t/ /\Q$var\E/; # is equivalent to /bla\[abc\]\\t/
In reply to Re: Problem with interpolating backslash
by Joost
in thread Problem with interpolating backslash
by ivanatora
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |