Perl: the Markov chain saw | |
PerlMonks |
Re: problem with optional capture groupby davido (Cardinal) |
on Dec 22, 2020 at 17:20 UTC ( [id://11125617]=note: 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.
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:
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 Section
Seekers of Perl Wisdom
|
|