my $str = "1*(2+3)*(3+4)+5*(6+7)"; $str =~ /(?: \( .+? \) | [^+()] )+ (\+ .*)/x; say "Match: $1"; #### $str = "1*(2+3)*(7+(3+4)+2)+5*(6+7)"; $str =~ /( \( (?: [^()]++ | (?1) )* \) | [^+()] )+ (\+ .*)/x; say "Match: $2"; #### /( # Capture group 1 \( # Opening paren (?: # Non-capture group [^()]++ # Match non-brackets, no backtracking | (?1) # Recurse to group 1. (?R) works, too. )* # Zero or more times \) # Closing paren | [^+()] # Anything outside of parens not a + or paren )+ # Not *, assuming you only want addition, not # +positive integers at beginning of string (\+ .*) # Plus sign, and remainder of string /x;