use strict; use warnings; my $data = do {local $/; }; if ($data =~ /(\s*{ [^{}]* \bfopen\b ((?: (?>[^{}]+) | {(?-1)} )*) })/x) { print $1; } __DATA__ void fo { ........ { //first one f = fopen(...); ....... if(...) { } } //mapping one ........ }