Perhaps a bit more fancy than necessary, but a lot is factored out. Needs Perl version 5.10+ for (*SKIP) (*FAIL) (see Special Backtracking Control Verbs in perlre):
c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $text = q{x aren't y ain't z isn't w won't u 't v daren't t}; print qq{'$text'}; ;; my $sq_t = qr{ 't }xms; my $skip_sq_t = qr{ aren | isn }xms; ;; my $exceptions_sq_t = qr{ \b $skip_sq_t $sq_t (*SKIP) (*FAIL) }xms; ;; $text =~ s{ $exceptions_sq_t? (?<= [[:alpha:]]) (?= $sq_t) }{ }xmsg; print qq{'$text'}; " 'x aren't y ain't z isn't w won't u 't v daren't t' 'x aren't y ain 't z isn't w won 't u 't v daren 't t'
Update: Another, more highly factored approach. See haukex's article Building Regex Alternations Dynamically for more info on how the $exception_n_sq_t regex is built. Something like this could be used to manufacture countless target/exception regex pairs.
c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $sq_t = qr{ 't }xms; ;; my $target_n_sq_t = qr{ (?<= [[:alpha:]]) (?= $sq_t) }xms; ;; my ($exception_n_sq_t) = map qr{ \b (?: $_) n $sq_t (*SKIP) (*FAIL) }xms, join q{ | }, qw(are is) ; print $exception_n_sq_t; ;; my $text = q{x aren't y ain't z isn't w won't u 't v daren't t}; print qq{'$text'}; ;; $text =~ s{ $exception_n_sq_t? $target_n_sq_t }{ }xmsg; print qq{'$text'}; " (?msx-i: \b (?: are | is) n (?msx-i: 't ) (*SKIP) (*FAIL) ) 'x aren't y ain't z isn't w won't u 't v daren't t' 'x aren't y ain 't z isn't w won 't u 't v daren 't t'
Give a man a fish: <%-{-{-{-<
In reply to Re: substitution with exceptions
by AnomalousMonk
in thread substitution with exceptions
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |