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


In reply to Calling sub-routine in regex by tonydunn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.