in reply to removing words

Rather than using captures and substituting with $1$2 I would probably use look-behind and look-ahead assertions so that I can substitute just my stop word with nothing.

use strict; use warnings; my @stopWords = qw{the on}; my $title = q{The% cat sat 4on5 the %tHe% hat %On}; print qq{Title : $title\n}; foreach my $stopWord (@stopWords) { print qq{Weeding : $stopWord\n}; # Do substitution using extended regular # expression syntax to allow comments in # the pattern. # $title =~ s{(?x) # Substitute ... (?: # Alternation for look-behind (?<=\A) # If preceded by string start | # or (?<=[%0-9]) # percent or digit ) # Close alternation (?i:$stopWord) # ... ignoring case, stop word ... (?=[%0-9]|\z) # If followed by percent or # digit, or string end }{}g; # ... with nothing globally print qq{Title : $title\n}; }

The output is

Title : The% cat sat 4on5 the %tHe% hat %On Weeding : the Title : % cat sat 4on5 the %% hat %On Weeding : on Title : % cat sat 45 the %% hat %

I hope this is of use.

Cheers,

JohnGG