Just use a proper DOM parser and be done with it. You don't have to work extra hard to produce an inferior regex solution to a problem that has already been solved robustly and that has a solution with very low barrier to entry.

#!/usr/bin/env perl use strict; use warnings; use Mojo::DOM; use Mojo::Util 'trim'; my $html = do {local $/ = undef; <DATA>}; my $dom = Mojo::DOM->new($html); foreach my $div ($dom->find('div')->each) { printf "Found div with id [%s], class [%s] and content [%s]\n", $div->{'id'} // '', $div->{'class'} // '', trim($div->content // ''); } __DATA__ <div id="roguebin-response-35911" class="bin-response"></div> <div id="othertest-1" class="foobar">content here </div>

Notice the second div spans more than one line. This is a problem you don't have to solve yourself. It may be relatively trivial to do so, but this is only the first of many problems people encounter using regexes to treat HTML as regular text when it's not.

Here's the output:

Found div with id [roguebin-response-35911], class [bin-response] and +content [] Found div with id [othertest-1], class [foobar] and content [content h +ere]

Installing Mojolicious will set you back two megabytes of storage once installed, and will also provide you with a UserAgent, a web framework, and a testing tool. You can install it using cpanm, or by downloading the tarball from Mojolicious, unpacking it, and running perl Makefile.PL && make && make test && make install. In containers, Carton is a nice way of specifying the dependency. Install time takes about a minute, and has no non-core Perl requirements, although you do need to be on a version of Perl less than six years old.

If you want to trigger some behavior based on whether the div is on a different line, just search $div->content for \n. But unless you're writing some sort of tool for cleaning up HTML it's usually best to write code that doesn't care about the formatting of the HTML.


Dave


In reply to Re: problem with optional capture group by davido
in thread problem with optional capture group by Special_K

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.