in reply to a question about re

My first solution did not take into account cases where statements before the fopen but on the same block level. This was mostly because it would lead to the regex being even more complicated and I figured my example would lead you in the right directly if you wanted to go the regex route.

However, upon closer inspection and trying to do the problem myself, I ran into efficiency problems with the regex never completing. It took quite a little finagling to get it to work, so I'm also sharing this solution:

use strict; use warnings; my $data = do {local $/; <DATA>}; our $braces_re = qr/(\{ (?: (?>[^{}]+) | (?-1) )* \})/x; while ($data =~ /(\{ (?: (?>(?:(?!\bfopen\b)[^{}])+) | (?> $braces_re ) )* \bfopen\b (?: (?>[^{}]+) | (?> $braces_re ) )* \})/xg) { print "'$1'\n"; } __DATA__ void fo { ........ { //first one f = fopen(...); ....... if(...) { } } //mapping one ........ } void bar { ........ { //first one if(...) { for { if { } else { } } f = fopen(...); while { } } ....... if(...) { } } //mapping one ........ }