$word = qr{ \w [\w'-]* }x; $nonword = qr{ [^\w'-]+ }x; $text =~ s{ \b ($word) (?: $nonword \1 )+ (?! \w ) # UPDATE }{$1}xg;