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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.