regex challange: collapse nested {} blocks

K

ken

Okay, I thought this might be a problem that has been addressed, but
Google didn't lead me to anything. I have source code files with blocks
denoted by {}. For exapmle:

loop a1 {
cmd;
loop a2 {
cmd;
cmd;
}
}
loop b1 {
cmd;
...
}

I want hide to (or collapse) all code blocks at a depth > N.
For example, N=1 would collapse loops a1, a2, b1 and output:

loop a1 {}
loop b1 {}

N=2 would only collapse loop a2 in this example:

loop a1 {
cmd;
loop a2 {}
}
loop b1 {
cmd;
...
}

I'd like to get something where N is just a variable in the Perl script,
but for now I'm already stuck just trying to get the N=1 case.

#!/usr/bin/perl
undef $/;
while(<STDIN>) {
s/{.*}/{}/gms;
print;
}

I see why this does not work, but not sure how to proceed. Any help
would be appreciated. Thanks,
Ken
 
T

Tad McClellan

ken said:
Okay, I thought this might be a problem that has been addressed,


It is a Question that is Asked Frequently. :)

I have source code files with blocks
denoted by {}.
I want hide to (or collapse) all code blocks at a depth > N.

Any help
would be appreciated.


perldoc -q balanced

Can I use Perl regular expressions to match balanced text?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top