use strict; use warnings; my $ariths = quotemeta '()+<-'; while ( ) { s/(?:(?:(?<=[$ariths])BAD)|^BAD)(?=[$ariths])/HELL/g; print; } __DATA__ +BAD # No BAD/ # Yes BAD) # Yes (BAD<) # Yes BAD # No BADGER # No BADGER+ # No +SINBAD+ # No BAD (BAD+ # No then Yes