#! /usr/local/bin/perl use strict; use warnings; my $str = "word1, word2, #18abcdefgh ,word4, #24qwer, word5"; print "Before: $str\n"; while($str =~ m/(.*?)#(\d)(\d)(.*)/) { $str = $1 . "" . substr($4, $3); } print "After: $str\n";