$line =~ s/.{2}\s+/:/; #### $line = 'this has three spaces'; #before $line = 'this:has:three spaces'; #after