#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::DOM; my $html = '

2.1. Match Literal Text

2.2. Match Nonprintable Characters

2.3. Match One of Many Characters

'; my $dom = Mojo::DOM->new( $html ); #for each hr foreach my $hr ( $dom->find('h2')->each ){ # grab the text my $class = $hr->text; # remove x.x. $class =~ s/\-?\d+\.\d+\. //g; # update the DOM $hr->attr('class' => $class ); } # display updated DOM say $dom;