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
}
}
}
}