#!/usr/bin/perl use strict; use warnings; { local $/=''; while (my $line = ){ my $i=0; while ($line =~ m//) { $line =~ s!!''!ie; #why not insert another substitution in here that handles the case ... } print $line; } } __DATA__ 123 1and23 12and3 123