Yes it can, but your s/// fails on both accounts: it doesn't match duplicated letters, and it doesn't keep track of what character that was. Here is code that matches duplicated letters and replaces the second one with a '2':
$string =~ s/([a-zA-Z])\1/${1}2/g;
Note these things:
- [a-zA-Z] matches a lowercase or uppercase English letter
- ([a-zA-Z]) captures that letter to the backreference \1 (and later, to $1)
- \1 matches whatever is stored in backreference \1 (the specific letter matched)
- ${1}2 is the variable $1 (how you retrieve backreference \1 outside a regex) followed by the number 2 -- the reason I have $1 in braces (${1}) is so that Perl doesn't see $12 as the scalar variable $12
If you don't care about case, you can use the
/i modifier, like so:
$string =~ s/([a-z])\1/${1}2/ig;
Please read
perlretut for more.