I did something like this before, and I really like to split. This may or may not suit your needs, but it's the way I'd approach it without Parse::RecDescent (which I really like). It's your stuff firs, then mine.
#!/usr/bin/perl -wl ## note the -l flag use strict; ## your stuff my $string = q( Outside. {tag} Inside level 1. {tag} Inside level 2. {/tag} Inside level 1. {/tag} Outside. ); ## original input print $string; while($string =~ s/\{tag\}(.+)\{\/tag\}/--Marked--\n$1\n--EndMarked--/ +gis) { next; } ## transformed output print $string; ## my stuff print "#" x 69; my %tags = ( 'Tagged' => 'tag', 'Bagged' => 'bag', ,); $string = q( Outside. {tag} Inside level 1. {tag} Inside level 2. {bag} bag level 1, inside tag level 2 {/bag} {/tag} Inside level 1. {/tag} Outside. ); my $Sfactor = join '|', map {quotemeta} map {("{$_}","{/$_}")} values +%tags; my @thing = split m/($Sfactor)/, $string; for my $t(@thing) { for my $tag(keys %tags) { $Sfactor = quotemeta "{$tags{$tag}}"; $t =~ s/$Sfactor/--$tag--/; $Sfactor = "{/$tags{$tag}}"; $t =~ s/$Sfactor/--End$tag--/; } } $string = join '', @thing; ## back as a string print $string; __END__ F:\dev\>perl markup.pl Outside. {tag} Inside level 1. {tag} Inside level 2. {/tag} Inside level 1. {/tag} Outside. Outside. --Marked-- Inside level 1. --Marked-- Inside level 2. --EndMarked-- Inside level 1. --EndMarked-- Outside. ##################################################################### Outside. --Tagged-- Inside level 1. --Tagged-- Inside level 2. --Bagged-- bag level 1, inside tag level 2 --EndBagged-- --EndTagged-- Inside level 1. --EndTagged-- Outside. F:\dev\>

 
___crazyinsomniac_______________________________________
Disclaimer: Don't blame. It came from inside the void

perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"


In reply to (crazyinsomniac) Re: Properly transforming strings with nested markup tags by crazyinsomniac
in thread Properly transforming strings with nested markup tags by tocie

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.