Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:50:56+00:00 2026-06-15T02:50:56+00:00

Hi i’m using HTML::TreeBuilder / HTML::Element to clean up some bad HTML produced by

  • 0

Hi i’m using HTML::TreeBuilder / HTML::Element to clean up some bad HTML produced by programs such as Microsoft Word.

Given the snippet of bad HTML in the example I need to extract the text between mosh="start" and mosh="stop". Note this is an arbitrary attribute set elsewhere in the code.

Also note that this is only an example: the only guarantee are the div where mosh starts and stops. These could also be tables or <p><b>.

The code below achieves this but each line is extracted multiple times because each child also has children.

$MoshText should be

Good Text can be pattern matched Wanted Text More Wanted TextYet More Wanted Text

But after the table

$MoshText is

Good Text can be pattern matched Good Text can be pattern matched Good Text can be pattern matched Good Text can be pattern matched

I then need to split $MoshText on m/matched/ into two strings and remove whatever object the original text was in.

How can I modify the code below to achieve this?

#!/usr/bin/perl
use HTML::TreeBuilder;
use HTML::Element;

my $body =qq(
<body>
  <div mosh="start">Div where mosh set to start</div
  <div>
<table>
  <tr>
    <td></td><td</td>
    <th>Good Text can be pattern matched</th>
    <td></td><td</td>
  </tr> 
</table
</div>
<p>

   <p>
      <b>Wanted Text</b>
   <br>
      <p><b>More Wanted Text</b></p>
   <div>
      <p><b>Yet More Wanted Text</b></p>
   </div>
  </p>
<div mosh="stop">Div where mosh set to stop bellow here is not needed</div>
);

my ($MoshText, $Flag);

my @kids = $body->content_list();
while (@kids) {
    my $child = shift @kids;
    if (ref $child) {
        my $Mosh = child->attr("mosh");
        if ($Mosh eq "start") {
            $Flag = 1;
        }
        if ($Mosh eq "stop") {
            $Flag = 0;
            last;
        }
        if ($Flag == 1) {
            my $T = $child->as_trimmed_text;
            $MoshText = $MoshText . " " . $T;
        }
        unshift @kids, $child->content_list;
    }
}
print $MoshText . "\n";

EDIT

To clarify what I meant by remove whatever object the original text was in

the table containing “Good Text can be pattern matched” shouldn’t be in a table but a div

I’m amusing is an object so I’d replace this object with a new div object something like

my $new = HTML::Element->new('tag','div');
$new->attr('class', 'MyClass');
$new->push_content('Good Text can be pattern matched');

but how would i now find the table delete and insert $new

Cleaned Output

    <div>
      Div where mosh set to start
    </div> 
    <div class ='MyClass'>
      Good Text can be pattern matched
    </div>
    <div class ='AnotherClass' >
      Wanted Text More Wanted Text Yet More Wanted Text
    </div>
    <div mosh="stop">Div where mosh set to stop bellow here is not needed</div>

Hope that makes more sence

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:50:57+00:00Added an answer on June 15, 2026 at 2:50 am

    I think you understand why your code isn’t working. You are printing the text value of all the elements in the HTML, and because an element’s text value includes all of its descendants’ text nodes several pieces of text are appearing more than once.

    You need to process the HTML tree recursively, checking the value of the mosh attribute for each element and keeping a flag accordingly (as you already do) and printing text nodes as you come across them only if the flag is set.

    This program demonstrates. I have shown splitting the string on matched, but I’m not clear what you mean by remove whatever object the original text was in.

    use strict;
    use warnings;
    
    use HTML::TreeBuilder;
    use HTML::Element;
    
    my $tree = HTML::TreeBuilder->new->parse_file(*DATA);
    
    my $wanted;
    my @mosh_text;
    my @nodes = ($tree);
    
    while (@nodes) {
    
      my $node = shift @nodes;
    
      if (not ref $node) {
        push @mosh_text, $node if $wanted;
      }
      else {
    
        my $mosh = lc($node->attr('mosh') // '');
        if ( $mosh eq 'start' or $mosh eq 'stop' ) {
          $wanted = $mosh eq 'start';
        }
    
        unshift @nodes, $node->content_list;
      }
    }
    
    my $mosh_text = "@mosh_text";
    print "$_\n" for split/\s*matched\s*/, $mosh_text;
    
    __DATA__
    <body>
      <div mosh="start">Div where mosh set to start</div
      <div>
    <table>
      <tr>
        <td></td><td</td>
        <th>Good Text can be pattern matched</th>
        <td></td><td</td>
      </tr> 
    </table
    </div>
    <p>
    
       <p>
          <b>Wanted Text</b>
       <br>
          <p><b>More Wanted Text</b></p>
       <div>
          <p><b>Yet More Wanted Text</b></p>
       </div>
      </p>
    <div mosh="stop">Div where mosh set to stop bellow here is not needed</div>
    

    output

    Div where mosh set to start Good Text can be pattern
    Wanted Text More Wanted Text Yet More Wanted Text
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.