I dont know of any magic regex that can do this. You have to be careful about balancing and all sort of fun stuff like that. I did something like this for formatting some config files at my work and I used Text::Balanced with a twist of recursion. This may not be the best solution for you, but it might give you a start:
use Text::Balanced qw(gen_extract_tagged); my $extract = gen_extract_tagged( 'node\s+\w+\s+\{', # start tag '\}', # end tag '(?s).*?(?=node\s+\w+\s+\{)' # prefix ); # $text is your file contents print indent($extract, $text); sub indent { my( $extract, $text, $depth ) = @_; $text =~ s/^\s+//mg if $depth == 0; if( index($text, '{') == -1 ) { #no more blocks $text =~ s/^/" "x$depth/gme; return $text; } #extract block out of $text my($extracted, $remainder, $prefix, $start, $block, $end) = $extract->($text); $prefix =~ s/^/" "x$depth/gme; my $formatted = $prefix; $formatted .= " " x $depth . $start; # parse the inner block $formatted .= indent($extract, $block, $depth+1) if $block; $formatted .= " " x $depth . $end; # parse what is after this block $formatted .= indent($extract, $remainder, $depth) if $remainder; return $formatted; }

For an input $text of
node myContainer { someParam WhateverWeWerePassed node myNode { fooParam DogPoopMonk node foo2 { stuff more stuff even more stuff node mode2 { stuff } } } }


I got the results of:
node myContainer { someParam WhateverWeWerePassed node myNode { fooParam DogPoopMonk node foo2 { stuff more stuff even more stuff node mode2 { stuff } } } }

In reply to Re: Automagically indenting (implementation, code) by perlmonkey
in thread Automagically indenting (implementation, code) by deprecated

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.