http://qs321.pair.com?node_id=11125617


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.


Dave