The best advice already given is use a template module. If you decide not to, I'd use substr rather than regular expressions

You can use index to find closing braces, the first one you find will be the close of the innermost scope. Then use the position you found with rindex to find the opening brace of that scope. You can extract the data with substr.

You can process the extract and then use substr to replace it and the braces in the original data.

Instead of processing the extract and replacing it, this snippet uses substr to replace the braces. This means you won't continually work on the same scope. It prints the bits it extracted as $extract so you can see it works on scopes in the correct order.

You need to make sure that whatever you substitute in doesn't have braces.

#!/usr/bin/perl use strict; use warnings; my $data = <DATA>; chomp $data; while () { my ($rpos, $lpos) = (0, 0); $rpos = index($data, "}", $rpos); last if $rpos < 0; $lpos = rindex($data, "{", $rpos); last if $lpos < 0; # $lpos + 1 so we don't extract the the { # -1 at the end to exclude the } my $extract = substr($data, $lpos + 1, $rpos - $lpos - 1); # get rid of these braces. substr($data, $lpos, 1) = "<"; substr($data, $rpos, 1) = ">"; # if you wanted to replace the entire section including braces # substr($data, $lpos, $rpos - $lpos + 1) = "<>"; print "|${extract}|\n"; } print "|${data}|\n"; __DATA__ text text {scope 4 text {scope 2 text {scope 1 text} scope 2 text} sco +pe 4 text {scope 3 text} scope 4 text } text text
Output
|scope 1 text| |scope 2 text <scope 1 text> scope 2 text| |scope 3 text| |scope 4 text <scope 2 text <scope 1 text> scope 2 text> scope 4 text +<scope 3 text> scope 4 text | |text text <scope 4 text <scope 2 text <scope 1 text> scope 2 text> sc +ope 4 text <scope 3 text> scope 4 text > text text|

Nuance


In reply to Re: Text parsing. Processing scopes and subscopes. by nuance
in thread Text parsing. Processing scopes and subscopes. by Lana

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.