Q: How to remove specific characters from a string? I am brand new to Perl and struggling with it.
I need to learn just the basics and not much.
I googled and got no simple answer to my question.
I have a string which contains numbers, dots, dashes, colon: and two alphabets.
I want to replace one alphabet by a space and the other by nothing.
How do I do this?
Is there no string.ReplaceChar(theChar, replacement)?

A: You can try as in the example below:
From commandline:
To replace only the first occurrence of the alphabets:
sdlcb@ubuntu:~$ echo "123.-A456:7B9AB0" | perl -pe 's/A//; s/B/ /'
123.-456:7 9AB0

To replace all occurrences of the alphabets:
sdlcb@ubuntu:~$ echo "123.-A456:7B9AB0" | perl -pe 's/A//g; s/B/ /g'
123.-456:7 9 0

Within script:
#!/usr/bin/perl -w
use strict;

my $data = "123.-A456:7B9AB0";
my $final_data = $data;

$final_data =~ s/A// ;
$final_data =~ s/B/ /;

print "data: $data\n";
print "final_data: $final_data\n";

Use g for substituting all occurrences.

A: Perl lends itself to using regular expressions, so that is how I would approach it;
#!/usr/bin/perl -w
use strict;

my $ALPHABET = 'abcde';
my $source_data = "1.2.3-$ALPHABET:$ALPHABET";

my $dest_data = $source_data
$dest_data =~ s/(-)$ALPHABET(:)/$1 $2/;

print "source_data: $source_data\n";
print "dest_data: $dest_data\n";

--->
source_data: 1.2.3-abcde:abcde
dest_data: 1.2.3- :abcde

Here the reqular expresion operator (=~) is substituting the first occurance of the pattern (-)$ALPHABET(:) with a '- :' string.
This pattern is using capture groups '()' to locate the match within the data.
Depending on your data you will likely need to adjust the patterns in the capture groups.
The backrefernces $1 and $2 within the match are used for demonstration purposes.
We could help with a more specific regex if you include example data in your question.
