Hello, this is my first attempt at parsing an HTML file. What I am trying to do is to substitute a string in the textual part of the HTML file. I can do this quite easily with the following code. What I am missing is probably the most easiest part: I do not want to extract and substitute the textual part (like now). What I want is to substitute my string and print out the original HTML (with changed part, of course). How should I proceed? (In my real-life example, I will need to match my string over two $sentence, but I leave this level of complexity for a later stage).

use strict; use warnings; use HTML::TreeBuilder; my $str = "<ul>" . "<div class=\"txt\" style=\"position:absolute; left:84px; top:73p +x;\"><span id=\"f1\" style=\"font-size:11px;vertical-align:baseline;c +olor:rgba(0,0,0,1);\">technology of S2S translation, also known as Sp +oken Language Translation (SLT),</span></div>" . "<div class=\"txt\" style=\"position:absolute; left:44px; top:73p +x;\"><span id=\"f1\" style=\"font-size:11px;vertical-align:baseline;c +olor:rgba(0,0,0,1);\">is a new application of AI,</span></div>" . "<li>there</li>" . "<li>everyone</li>" . "</ul>" ; # Now create a new tree to parse the HTML from String $str my $tr = HTML::TreeBuilder->new_from_content($str); # And now find all <li> tags and create an array with the values. my @lists = map { $_->content_list } $tr->find_by_tag_name('span'); # And loop through the array returning our values. foreach my $sentence (@lists) { $sentence =~ s/Spoken/SUBSTITUTION/; print $sentence, "\n"; }

In reply to substitution in textual area of HTML file by Takamoto

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.