tonydunn has asked for the wisdom of the Perl Monks concerning the following question:
I have a fairly large HTML file that I want to process to make useful with linking. As it stands, it's just 7.k of ordered lists with a sort of ToC, but none of which links together. My idea was to grep out lines by <h2-6> headers, to get something like:
<h2>2.1. Match Literal Text</h2> <h2>2.2. Match Nonprintable Characters</h2> <h2>2.3. Match One of Many Characters</h2>
Next step would be to search and substitute with regex, and harvest the existing text (after the ordered list tag) and make that into an ID, so I would end up with something like:
<h2 id="Match Literal Text">2.1. Match Literal Text</h2> <h2 id="Match Nonprintable Characters">2.2. Match Nonprintable Charact +ers</h2> <h2 id="Match One of Many Characters">2.3. Match One of Many Character +s</h2>
I did this like this:
use Tie::File; my $ol_file = '/Users/tony/Dropbox/perl_data/h2_numbered_list.txt'; my @ol_file_array; tie @ol_file_array, 'Tie::File', $ol_file or die "Something is wrong h +ere"; my $regex = qr/ (?<tag>(<\w\d)) (?<closing_bracket>>) (?<ol_numbers>(\d\.(\d+?)\.))\s (?<text>(.+)) (?<closing_tag>(\b<\/\w\d>)) /xms; foreach (@ol_file_array) { if ($_ =~ m/$regex/g) { $_ =~ s/$regex/$+{tag} id="$+{text}"$+{closing_bracket}$+{ol_n +umbers} $+{text}$+{closing_tag}/; } else { # blah blah blah
The problem I have now is that the text I harvested for the ID has spaces, and is (mostly) Title Case. What I want to do, and for which I seek your wisdom, is convert the text in the id to:
<h2 id="match_nonprintable_characters">2.2. Match Nonprintable Characters</h2>My initial thought was to write a sub-routine to take the ID text after the first pass alteration, and run it through something akin to lc() and s/ /_/, when running a regex sub. This is where I am stymied. I thoght I could write a sub called (say) 'changer' and insert it into the substitute regex, working on the capture group, like:
$_ =~ s/$regex_2/$+{group_x}$+{group_y} changer($+{text})/e;Mixed results with this, none right. The '/e' flag gets me a non-descript 'syntax error'. The call to the sub (if I remove the 'e' flag) does not (but not an error seemingly) but the result is typically like:
some_data some_data changer($+{text})In other words, the sub-routine does not work, and the text is returned in the regex sub literally.
I've had a look online and in my Perl books library, and I think I've understood that the right-side of the s/// needs to be code only, because it is compiled first, but I am failing on how I can get this done, for my scenario.
Is this clear? I'd welcome any suggestions from more knowledgeable programmers. The idea once this works in principle is to run the whole HTML file through it, and auto-generate a ToC and links, using the text that is already in place.
Many thanks.
Tony
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Calling sub-routine in regex
by marto (Cardinal) on Jul 19, 2020 at 16:21 UTC | |
Re: Calling sub-routine in regex
by marto (Cardinal) on Jul 19, 2020 at 18:09 UTC | |
Re: Calling sub-routine in regex
by LanX (Saint) on Jul 19, 2020 at 15:50 UTC | |
Re: Calling sub-routine in regex
by kcott (Archbishop) on Jul 20, 2020 at 06:37 UTC | |
| |
Re: Calling sub-routine in regex
by BillKSmith (Monsignor) on Jul 19, 2020 at 17:47 UTC | |
Re: Calling sub-routine in regex
by tonydunn (Novice) on Jul 21, 2020 at 13:26 UTC | |
by marto (Cardinal) on Jul 21, 2020 at 13:28 UTC | |
Re: Calling sub-routine in regex
by BillKSmith (Monsignor) on Jul 26, 2020 at 18:32 UTC |