user2131116
user2131116

Reputation: 2861

How to use regular expression to remove $ character in string?

I want to use regular expression to remove string with $ , % , # these three characters , but it seems can't remove $ and the error information shows undefined variable

How can I solve this problem?

here is my code

perl Remove.pl $ABC#60%


#!/usr/bin/perl

$Input = $ARGV[0];

$Input =~ s/\$|%|#//g;

print $Input;

thanks

Upvotes: 1

Views: 170

Answers (3)

akawhy
akawhy

Reputation: 1608

if you just want to remove some charactor, it will be better use tr

try this:

perl -e '$arg = shift; $arg =~ tr/$%#//d; print $arg' '$asdf#$'

your code is just fine, but the parameter you pass to the program will expand in bash. you should put single quote.

try this:

perl Remove.pl '$ABC#60%'

Upvotes: 1

perreal
perreal

Reputation: 98088

I think your problem is with the shell, not with the Perl code. Single quote the argument to the script:

perl remove.pl '$ABC#60%'

The shell can interpret '$ABC' as a variable name in which case the script will receive no arguments. Perl will then complain about undefined variable in substitution.

Upvotes: 6

William Pursell
William Pursell

Reputation: 212474

$Input =~ s/[\$%#]//g;

ought to work

Upvotes: 1

Related Questions