Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a text below and along with this there is some more text which is p tags, br tags. Please help me to remove entire sentence if contains 23456.
<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 ph +one. Text 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

Replies are listed 'Best First'.
Re: regex help
by i5513 (Pilgrim) on Aug 02, 2015 at 08:00 UTC
Re: regex help
by Laurent_R (Canon) on Aug 02, 2015 at 08:29 UTC
    Hi;

    you should at least explain better what you really need, provide some context or some code that fails to do what you want. Without any detail, your problem might be solved with something as simple as:

    while (<$fh>) { next if /23456/; # further processing of lines not containing 23456 }
    Or it could be more complicated, we cannot know.
Re: regex help
by Corion (Patriarch) on Aug 02, 2015 at 05:53 UTC

    What code have you already written and where does it fail?

Re: regex help
by james28909 (Deacon) on Aug 02, 2015 at 16:59 UTC
    use strict; use warnings; while(my $html = <DATA>){ $html =~ s/<.*?>//g; next if ($html =~ /23456/); print $html; } __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 ph +one. Text 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
    In your input data, 23456 is in every line. This above will strip tags and then skip lines with 23456 in it. But with the input data you posted, it wont return any data because every line contains 23456.

    You could add:
    $html =~ s/23456/123456789/g;
    to the above code and it will search and replace 23456. But i am really unsure why you want to skip all lines containing 23456, because all the lines in your input data contain 23456.
Re: regex help
by 1nickt (Canon) on Aug 02, 2015 at 17:03 UTC

    When you say "remove entire sentence" do you mean "remove entire sentence from input data and keep it in output?"

    If so, a better word for it is "retrieve."

    The way forward always starts with a minimal test.
Re: regex help
by Athanasius (Archbishop) on Aug 03, 2015 at 07:02 UTC

    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,