Here’s my take on the requirement (and, admittedly, it’s only a guess): Identify sentences containing the target string 23456 and delete them, leaving the other sentences — together with their surrounding markup — unchanged.

I came up with this algorithm:

  1. Slurp the input file in as a single string.
  2. Extract sentences:
    1. Remove markup tags.
    2. Extract each “sentence” as a minimal sequence of characters beginning either at the beginning of the string or following the whitespace at the end of the previous sentence, and terminated by a full stop, a question mark, or an exclamation mark, which is in turn followed either by whitespace or by the end of the string.
  3. Filter out sentences which do not contain the target string.
  4. Delete the remaining sentences (i.e., those which do contain the target string) from the original input string, thus leaving the original markup unaffected.

The trickiest part of this is identifying sentences. Here’s my solution:

#! perl use strict; use warnings; my $original = do { local $/; <DATA>; }; my $string = $original =~ s{ < /? (?: p | br) > }{}grx; my @sentences = $string =~ m{ ( .*? (?: [.?!] \s+ | \z) ) }gsx; chomp @sentences; $original =~ s{$_}{} for grep { /23456/ } @sentences; $original =~ s{ +}{ }g; print $original; __DATA__ <p>Find more business news at facebook.com and twitter.com. Text BUSIN +ESS to 23456 for breaking business news text alerts on your mobile phone. Tex +t JOBS to 23456 for job alerts.</p> <p>Want your news even faster? Text NEWS to 23456 to sign up for break +ing news text alerts. See for a complete list of alerts.</p> <br>SIGN UP FOR MOBILE NEWS ALERTS! Get your news on the go, text NEWS + to 23456

Output:

16:49 >perl 1326_SoPW.pl <p>Find more business news at facebook.com and twitter.com. </p> <p>Want your news even faster? See for a complete list of alerts.</p> <br>SIGN UP FOR MOBILE NEWS ALERTS! 16:49 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: regex help by Athanasius
in thread regex help 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.