#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1139932 use strict; use warnings; my $in = 'text text {scope 4 text {scope 2 text {scope 1 text} scope 2 text} scope 4 text {scope 3 text} scope 4 text } text text'; print $_ = $in; print while s/{[^{}]*}/PROCESSED/;