Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here is something to get you started. Using Mojo::DOM, parse the HTML, match a single sentence then alter the DOM to do what you want:

#!/usr/bin/perl use strict; use warnings; use Mojo::DOM; # slurp in from file, or get using Mojo::UserAgent... my $html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional// +EN"> <html> <body> <p>This is sentence 1</p> <p>This is <font color="#000000"><font face="Century Gothic, serif">se +ntence 2</font></font></p> <p>This is sentence 1. This is sentence <span style="font-weight: norm +al">3</span> </p> </body> </html>'; # new Mojo::DOM my $dom = Mojo::DOM->new( $html ); # for each p tag found for my $e ( $dom->find('p')->each ){ # use the all_text method to get all of the visible text, including +from # descending tags. In this short example to get you started match a +specific # string. if ( $e->all_text eq 'This is sentence 2' ){ # once we have a match, wrap the node around this: $e->wrap_content('<sentence id="2"></sentence>'); } } # print to screen, do whatever you want with the results. print $dom->content;

Produces:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <body> <p>This is sentence 1</p> <p><sentence id="2">This is <font color="#000000"><font face="Century +Gothic, serif">sentence 2</font></font></sentence></p> <p>This is sentence 1. This is sentence <span style="font-weight: norm +al">3</span> </p> </body> </html>

Now all you have to do is make this generic, but that's fairly trivial, and I'll leave that as an exercise for you.

See also Re: Batch remove URLs or Super Search for more examples.

Update: I assume the ordering of the second p/sentence tag here was a mistake, closing the p tag before the sentene:

<p><sentence id=1>This is sentence 1></sentence></p> <p><sentence id=2>This is <font color="#000000"><font face="Century +Gothic, serif">sentence 2</font></font></p></sentence> <p><sentence id=3>This is sentence 1. </sentence><sentence id=4>Th +is is sentence <span style="font-weight: normal">3</span></sentence>< +/p>

In reply to Re: Match text from txt to html by marto
in thread Match text from txt to html by corfuitl

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (6)
    As of 2019-12-13 21:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?