#!/usr/bin/perl use Text::Balanced 'extract_bracketed'; $_ = <<'#END#'; This stays onValidate { stuff in here { other stuff } } This stays too #END# if(/onValidate/) { print substr($_, 0, $-[0]); # before my ($extracted, $remainder) = extract_bracketed(substr($_, $+[0]), '{}'); print $remainder; # after print "\n## extracted: ##\n$extracted\n"; }