*UPDATE* It occurred to me that my code will cause a runtime error or weird behaviour if any element in @list contains regex metachars as these will be interpolated into the eat it up regex. To fix this we need to escape all these chars. Here is the patched code.

tachyon

my $html = "<h1>F(oo</h1><p>Bar</p><p>Some more text here</p>"; my @list = ('F(oo','Bar'); # you need to make the elements in @list regex # friendly by backslashing all the metachars # comment out this line to see this script choke # on the ( in F(oo s/([\$\^\*\(\)\+\{\[\\\|\.\?])/\\$1/g for @list; # eat up the bits in @list $html =~ m/$_/gc for @list; #use \G to match the rest ($rest) = $html =~ m/\G(.*)$/; print $rest;

*Update* added \) which slipped throught the net. Caught by chipmunk. chipmunk also points out that quotemeta is a good solution but my pride won't allow me to use it because it is both shorter and more elegant!

# s/([\$\^\*\(\)\+\{\[\\\|\.\?])/\\$1/g for @list; $_ = quotemeta $_ for @list;

In reply to Re: getting the first n printable words from a string of HTML by tachyon
in thread getting the first n printable words from a string of HTML 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.