$a = 'xx|xx|xx'; # should this be (use example above): [xx|x x|xx] # or should it be [x x|x x|x x] # in which case you will need to add an extra regex because you can't have # variable width lookbacks. this regex just processes those edge cases $a =~ s/^xx(?=\|)|(?<=\|)xx$/x x/g; # combined with the first part gives you some real perl line noise # if you want it all in a single regex $a =~ s/^xx(?=\|)|(?<=\|)xx(?=\|)|(?<=\|)xx$/x x/g;