in reply to Efficient string tokenization and substitution
or ( if you have a lot of tokens this might be faster )$string =~ s/([^\s\.\]\[]+)/exists($tokens_to_match{lc $1}) ? $tokens_to_match{lc + $1} : $1/ge;
my $str = join '|', sort { length $b <=> length $a || $a cmp $b } keys %tokens_to_match; $string =~ s/(\b|\s|\.|\[|\])($str)(?>(\b|\s|\.|\[|\]))/ $1 . ( exists($tokens_to_match{lc $2}) ? $tokens_to_match{lc $2} : $2 ) /gei;
|
|---|