Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Regexp for HTML

by marto (Cardinal)
on Jan 12, 2024 at 20:42 UTC ( [id://11156929]=note: print w/replies, xml ) Need Help??


in reply to Regexp for HTML

For clarity, all of the impacted links have a distinct widget in the href? If so do you have a string you'd like to replace it with?

Replies are listed 'Best First'.
Re^2: Regexp for HTML
by gossamer (Sexton) on Jan 12, 2024 at 20:54 UTC
    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?

      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://11156929]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-05-01 03:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found