my $braces = qr{ \( (?: (?> [^\(\)"]+ ) # Non-parens without backtracking | (?> "[^"]*")+ # skip string constants | (??{ $braces }) # Group with matching parens )* \) }x; my $Braces = qr{\(((?:[^\(\)"]+|"[^"]*")*(?:$braces(?:[^\(\)"]+|"[^"]*")*)*)\)}; my $str = 'foo( FieldName, trim(CStr(Request.Form(FieldName))), CountryId )'; $str =~ $Braces and print "YES: $1\n" or print "NO!\n";