Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Regexp for HTML

by gossamer (Sexton)
on Jan 12, 2024 at 20:54 UTC ( [id://11156930]=note: print w/replies, xml ) Need Help??


in reply to Re: Regexp for HTML
in thread Regexp for HTML

I would like to remove the entire string. Maybe an option would be to do a simple replace of the "widget=\d+" with an actual URL then use the regular HTML parser tools to delete the string?

Replies are listed 'Best First'.
Re^3: Regexp for HTML
by marto (Cardinal) on Jan 12, 2024 at 21:49 UTC

    Here's an example using Mojo::DOM, to make life easy, even with borked HTML. It replaces hrefs beginning with the selector ;widget=460, replacing them with the URL for this site.

    #!/usr/bin/perl use strict; use warnings; use Mojo::DOM; use feature 'say'; # slurp from file, get from a live site via Mojolicious::UserAgent etc +... # hardcoded for example purposes my $html = '<p><a href="https://example.com">example.com</a></p><p><a +href=";widget=460" rel="noopener" target="_blank"><img alt="Advertise +r" class="banner" height="60" src="/images/articles/newsletters/Paper +-v4-468x60.jpg" style="display: block; margin-left: auto; margin-righ +t: auto;" width="468" /></a></p>'; my $dom = Mojo::DOM->new( $html ); for my $url ( $dom->find('a[href^=";widget=460"]')->each ){ $url->attr('href' => 'https://perlmonks.org'); } say $dom->content;

    Output:

    <p><a href="https://example/com">example.com</a></p><p><a href="https: +//perlmonks.org" rel="noopener" target="_blank"><img alt="Advertiser" + class="banner" height="60" src="/images/articles/newsletters/Paper-v +4-468x60.jpg" style="display: block; margin-left: auto; margin-right: + auto;" width="468"></a></p

    Armed with this, it'd be trivial to have a list of widgets & their real urls, substitute the selector and static url value in the code above, looping through the list of widgets.

    Update: See also Re: Batch remove URLs, or super search for more examples.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-05-21 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found