|P is for Practical|
Re: Calling sub-routine in regexby kcott (Bishop)
|on Jul 20, 2020 at 06:37 UTC||Need Help??|
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):
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.