Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found