Hi, you are using the match operator in void context where you want to use a string or a compiled regular expression in a variable assignment:

perl -wE ' my $remove = q{<div><div class="blue"></div></div>}; my $str = q{</div></div><div><div class="blue"></div></div>}; say $str =~ s/$remove//r;'
Or: (update2: ++Laurent_R pointed out that I had the quote operators reversed for string and substring in my OP):
perl -wE ' my $remove = qr{<div><div class="blue"></div></div>}; my $str = q{</div></div><div><div class="blue"></div></div>}; say $str =~ s/$remove//r;'
See http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators.

Note that if you were using warnings Perl would have told you about this:

perl -wE ' my $remove = m/<div><div class="blue"><\/div><\/div>/;' Use of uninitialized value $_ in pattern match (m//) at -e line 1.

(update) As far as your second question, you have to either get the result of the substitution in list context, or use the /r ("result") flag as I have showed above:

$ perl -wE ' my $remove = q{foo}; my $str = q{barfoobaz}; say $str =~ +s/$remove//;' 1 $ perl -wE ' my $remove = q{foo}; my $str = q{barfoobaz}; ( my $x = $s +tr ) =~ s/$remove//; say $x;' barbaz $ perl -wE ' my $remove = q{foo}; my $str = q{barfoobaz}; say $str =~ +s/$remove//r;' barbaz

Also note that you can use any character as quote delimiters in order to avoid "leaning toothpick syndrome" (<\/div>).

Finally, also note that there are modules for working with HTML parsing and processing, and trying to do it yourself with regular expressions is not generally recommended as you are unlikely to anticipate and handle all the edge cases.

Hope this helps!

The way forward always starts with a minimal test.

In reply to Re: Strip specific html sequence by 1nickt
in thread Strip specific html sequence by koober

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.