>perl -wMstrict -le "my $prefix = qr{ (?i) \b tbgl_ }xms; my $word = qr{ \w+ }xms; my $suffix = '_gl'; ;; print '--- output ---'; for my $pw (@ARGV) { (my $ws = $pw) =~ s{ $prefix ($word) }{$1$suffix}xmsg; print qq{'$pw' -> '$ws'}; } " "tbgl_foo xxx %$# tbgl_bar()" "TBGL_foo tBgL_bar" "tbgl tbglfoo tbgl_" "tbgl_%$# Xtbgl_foo" --- output --- 'tbgl_foo xxx %$# tbgl_bar()' -> 'foo_gl xxx %$# bar_gl()' 'TBGL_foo tBgL_bar' -> 'foo_gl bar_gl' 'tbgl tbglfoo tbgl_' -> 'tbgl tbglfoo tbgl_' 'tbgl_%$# Xtbgl_foo' -> 'tbgl_%$# Xtbgl_foo'