Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: How to extract a pattern in Perl regex?

by marto (Cardinal)
on May 01, 2020 at 09:27 UTC ( [id://11116304]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to extract a pattern in Perl regex?
in thread How to extract a pattern in Perl regex?

Using Mojo::DOM (pulling live data use Mojo::UserAgent):

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::Util 'trim'; use Mojo::UserAgent; # get perlmonks my $ua = Mojo::UserAgent->new; my $dom = $ua->get('https://perlmonks.org')->res->dom; say 'Title: ' . trim( $dom->at('title')->text ); say 'Image src: ' . trim( $dom->at('img')->attr->{'src'} ); say 'Image alt: ' . trim( $dom->at('img')->attr->{'alt'} );

Output:

Title: PerlMonks - The Monastery Gates Image src: //promote.pair.com/i/pair-banner-current.gif Image alt: Beefy Boxes and Bandwidth Generously Provided by pair Netwo +rks

Mojo::DOM makes parsing fun and simple.

Replies are listed 'Best First'.
Re^4: How to extract a pattern in Perl regex?
by haukex (Archbishop) on May 02, 2020 at 09:23 UTC
    Mojo::DOM makes parsing fun and simple.

    Agreed, and ojo makes it even more fun ;-)

    $ perl -Mojo -e 'say g("https://perlmonks.org")->dom->at("title")->all +_text=~s/^\s+|\s+$//gr' PerlMonks - The Monastery Gates

      ojo is very nice, but I find myself more often writing programs that just aren't practical as one liners

        I find myself more often writing programs that just aren't practical as one liners

        Same here. My reaction was inspired by this: 'However part of my question was how do I extract in one statement what's in between the "title tags". ... Surely there's a simpler way?' I think one of the things that turns people off from using modules instead of plain regexes is because modules often require a lot of boilerplate, which luckily Mojolicious for the most part does not.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found