Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: problem with optional capture group

by davido (Cardinal)
on Dec 22, 2020 at 17:20 UTC ( #11125617=note: print w/replies, xml ) Need Help??

in reply to problem with optional capture group

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11125617]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2021-03-02 07:31 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (40 votes). Check out past polls.