No such thing as a small change | |
PerlMonks |
Re: Calling sub-routine in regexby kcott (Archbishop) |
on Jul 20, 2020 at 06:37 UTC ( [id://11119538]=note: print w/replies, xml ) | Need Help?? |
G'day Tony, Welcome to the Monastery. Using a regex to parse markup like HTML and XML is a poor idea (see "Why a regex *really* isn't good enough for HTML and XML, even for "simple" tasks"). Having said that, here's how you might have gone about this, using your three posted h2 lines (plus a couple more I added to test additional header levels):
Output:
Note that requires Perl 5.14 or later for the '/r' modifier (see "perl5140delta: Non-destructive substitution"). You can do something similar with older versions but it requires a bit more work. And, just for completeness, say was introduced in Perl 5.10. Also note that modifying elements of an array while you're looping through that array can have unforeseen problems. See how I loop through the indices of the array instead. — Ken
In Section
Seekers of Perl Wisdom
|
|