1

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

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

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

ought to work

Comments

1

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%'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.