my $a = "x|xx|xx|xxxx|xx|xxx|xx|xx|x"; $a =~ s/x\|xx\|x/x\|x x\|x/g; print "[$a]\n"; This gives the output [x|x x|xx|xxxx|x x|xxx|x x|xx|x]