Jaron787
Jaron787

Reputation: 560

Perl - can't remove trailing characters at the end of string

I have some trailing characters at the end of a string peregrinevwap^_^_

print "JH 4 - app: $application \n";

app: peregrinevwap^_^_

Do you know why they are there and how I can remove them. I tried the chomp command but this hasn't worked.

Upvotes: 0

Views: 1328

Answers (2)

mscha
mscha

Reputation: 6840

Are you sure these characters are actually ^ and _ characters? ^_ could also indicate Ctrl-Underscore, ASCII character 0x1F (Unit Separator). (Not a character I've ever seen used, but you never know.)

If this is in fact the case, you can remove them with something like:

$application =~ s/\x1F//g;

Upvotes: 0

neuhaus
neuhaus

Reputation: 4094

Check out the tr//cd operator to get rid of unwanted characters. It's documented in "perldoc perlop"

$application =~ tr/a-zA-Z//cd;

Will remove everything except letters from the string and

$application =~ tr/^_//d;

Will remove all "^" and "_" characters.

If you only want to remove certain characters when they at the end of the string, use the s// search/replace operator with regular expressions and the $ anchor to match the end of the string.

Here's an example:

s/[\^_]*$//;

Let's hope the underscores do not occur at the end of your strings, otherwise you can't automatically separate them from these unwanted characters.

Upvotes: 2

Related Questions