Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

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

Hi Anonymous Monk, try like this using Regular Expression,

use strict; use warnings; my $input = do{local $/; <DATA>}; $input =~ s/(<(h\d+>)[^\n]+)/$1<\/$2/gsi; $input =~ s/(<h(\d+)[^>]*>)(.*?)(?=(<h(\d+)[^>]*>))/"$1".&section_clos +e($2,$3,$5)/egsi; $input =~ s/(<(h\d+>)){2,}/$1/gsi; ######Last level if ($input =~/<h./si) { if ($input =~/(<h\d+[^>]*>)(.*)(<h(\d+)[^>]*>)(.*)$/si) { $input =~s/(<h\d+[^>]*>)(.*)(<h(\d+)[^>]*>)(.*)$/"$1$2$3".&sec +tion_close($4,$5,1)/egsi; } else { $input =~s/(.*)(<h(\d+)[^>]*>)(.*)$/"$1$2".&section_close($3,$ +4,1)/egsi; } } ############Heading replacement $input =~ s/(<h(\d+)>)((?:(?!<\/h\2>).)*)<\/h\2>/$1<head>$3<\/head>/gs +i; $input =~ s/(<\/?)h(\d+>)/$1section$2/gsi; print $input; sub section_close { my ($csect_no,$aft_txt,$asect_no)=@_; my $tag_close; if ($csect_no == $asect_no) { $tag_close="$aft_txt<\/h$csect_no>\n" } if ($csect_no < $asect_no) { my $j = $asect_no - $csect_no; my $i = $csect_no; my $temp = ""; while ($j > 1) { my $k = $i + 1; $temp = $temp."<h$k>\n"; $i++; $j--; } $tag_close = "<h$csect_no>".$aft_txt.$temp; } if ($csect_no > $asect_no) #head separation { my $temp = ""; my $i = $asect_no; for ($i = $asect_no; $i <= $csect_no; $i++) { $temp = "<\/h$i>\n".$temp; } $tag_close = $aft_txt.$temp; } return $tag_close; } __DATA__ <h1>Heading level 1 <h2>Heading level 2 <h3>Heading level 3 <h2>Heading level 2 <h1>Heading level 1 paragraph here paragraph here Output: ------- <section1><head>Heading level 1</head> <section2><head>Heading level 2</head> <section3><head>Heading level 3</head> </section3> </section2> <section2><head>Heading level 2</head> </section2> </section1> <section1><head>Heading level 1</head> paragraph here paragraph here </section1>

Velusamy R.

eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

In reply to Re: nested level section closing by Samy_rio
in thread nested level section closing by Anonymous Monk

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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (7)
    As of 2018-05-23 09:30 GMT
    Find Nodes?
      Voting Booth?