use re 'debug'; $np = qr{ \( (?: (?> [^()]+ ) # Non–parens without backtracking | (??{ $np }) # Group with matching parens )* \) }x; $funpat = qr/\w+$np/; "fun(1,(2),5)" =~ /^$funpat$/; #Matches!" #### Compiling REx "%n \(%n (?:%n (?> [^()]+ ) "... Final program: 1: EXACT <(> (3) 3: CURLYX[0] {0,32767} (28) 5: BRANCH (22) 6: SUSPEND (27) 8: PLUS (20) 9: ANYOF[\x00-'*-\xff][{unicode_all}] (0) 20: SUCCEED (0) 21: TAIL (26) 22: BRANCH (FAIL) 23: LOGICAL[2] (24) 24: EVAL (27) 26: TAIL (27) 27: WHILEM[1/1] (0) 28: NOTHING (29) 29: EXACT <)> (31) 31: END (0) anchored "(" at 0 floating ")" at 1..2147483647 (checking floating) minlen 2 with eval Compiling REx "\w+(?^x:%n \(%n (?:%n (?> ["... Final program: 1: PLUS (3) 2: ALNUM (0) 3: EXACT <(> (5) 5: CURLYX[0] {0,32767} (30) 7: BRANCH (24) 8: SUSPEND (29) 10: PLUS (22) 11: ANYOF[\x00-'*-\xff][{unicode_all}] (0) 22: SUCCEED (0) 23: TAIL (28) 24: BRANCH (FAIL) 25: LOGICAL[2] (26) 26: EVAL (29) 28: TAIL (29) 29: WHILEM[1/1] (0) 30: NOTHING (31) 31: EXACT <)> (33) 33: END (0) floating "(" at 1..2147483647 (checking floating) stclass ALNUM minlen 3 with eval Compiling REx "^(?^:\w+(?^x:%n \(%n (?:%n "... Final program: 1: BOL (2) 2: PLUS (4) 3: ALNUM (0) 4: EXACT <(> (6) 6: CURLYX[0] {0,32767} (31) 8: BRANCH (25) 9: SUSPEND (30) 11: PLUS (23) 12: ANYOF[\x00-'*-\xff][{unicode_all}] (0) 23: SUCCEED (0) 24: TAIL (29) 25: BRANCH (FAIL) 26: LOGICAL[2] (27) 27: EVAL (30) 29: TAIL (30) 30: WHILEM[1/1] (0) 31: NOTHING (32) 32: EXACT <)> (34) 34: EOL (35) 35: END (0) floating ")"$ at 2..2147483647 (checking floating) anchored(BOL) minlen 3 with eval Guessing start of match in sv for REx "^(?^:\w+(?^x:%n \(%n (?:%n "... against "fun(1,(2),5)" Found floating substr ")"$ at offset 11... Guessed: match at offset 0 Matching REx "^(?^:\w+(?^x:%n \(%n (?:%n "... against "fun(1,(2),5)" 0 <> | 1:BOL(2) 0 <> | 2:PLUS(4) ALNUM can match 3 times out of 2147483647... 3 <(1,(2),5)> | 4: EXACT <(>(6) 4 <1,(2),5)> | 6: CURLYX[0] {0,32767}(31) 4 <1,(2),5)> | 30: WHILEM[1/1](0) whilem: matched 0 out of 0..32767 4 <1,(2),5)> | 8: BRANCH(25) 4 <1,(2),5)> | 9: SUSPEND(30) 4 <1,(2),5)> | 11: PLUS(23) ANYOF[\x00-'*-\xff][{unicode_all}] can match 2 times out of 2147483647... 6 <(2),5)> | 23: SUCCEED(0) subpattern success... 6 <(2),5)> | 30: WHILEM[1/1](0) whilem: matched 1 out of 0..32767 6 <(2),5)> | 8: BRANCH(25) 6 <(2),5)> | 9: SUSPEND(30) 6 <(2),5)> | 11: PLUS(23) ANYOF[\x00-'*-\xff][{unicode_all}] can match 0 times out of 2147483647... failed... failed... 6 <(2),5)> | 25: BRANCH(29) 6 <(2),5)> | 26: LOGICAL[2](27) 6 <(2),5)> | 27: EVAL(30) Matching embedded REx "%n \(%n (?:%n (?> [^()]+ ) "... against "(2),5)" 6 <(2),5)> | 1: EXACT <(>(3) 7 <2),5)> | 3: CURLYX[0] {0,32767}(28) 7 <2),5)> | 27: WHILEM[1/1](0) whilem: matched 0 out of 0..32767 7 <2),5)> | 5: BRANCH(22) 7 <2),5)> | 6: SUSPEND(27) 7 <2),5)> | 8: PLUS(20) ANYOF[\x00-'*-\xff][{unicode_all}] can match 1 times out of 2147483647... 8 <),5)> | 20: SUCCEED(0) subpattern success... 8 <),5)> | 27: WHILEM[1/1](0) whilem: matched 1 out of 0..32767 8 <),5)> | 5: BRANCH(22) 8 <),5)> | 6: SUSPEND(27) 8 <),5)> | 8: PLUS(20) ANYOF[\x00-'*-\xff][{unicode_all}] can match 0 times out of 2147483647... failed... failed... 8 <),5)> | 22: BRANCH(26) 8 <),5)> | 23: LOGICAL[2](24) 8 <),5)> | 24: EVAL(27) Matching embedded REx "%n \(%n (?:%n (?> [^()]+ ) "... against "),5)" 8 <),5)> | 1: EXACT <(>(3) failed... BRANCH failed... whilem: failed, trying continuation... 8 <),5)> | 28: NOTHING(29) 8 <),5)> | 29: EXACT <)>(31) 9 <,5)> | 31: END(0) EVAL trying tail ... 0 9 <,5)> | 30: WHILEM[1/1](0) whilem: matched 2 out of 0..32767 9 <,5)> | 8: BRANCH(25) 9 <,5)> | 9: SUSPEND(30) 9 <,5)> | 11: PLUS(23) ANYOF[\x00-'*-\xff][{unicode_all}] can match 2 times out of 2147483647... 11 <)> | 23: SUCCEED(0) subpattern success... 11 <)> | 30: WHILEM[1/1](0) whilem: matched 3 out of 0..32767 11 <)> | 8: BRANCH(25) 11 <)> | 9: SUSPEND(30) 11 <)> | 11: PLUS(23) ANYOF[\x00-'*-\xff][{unicode_all}] can match 0 times out of 2147483647... failed... failed... 11 <)> | 25: BRANCH(29) 11 <)> | 26: LOGICAL[2](27) 11 <)> | 27: EVAL(30) Matching embedded REx "%n \(%n (?:%n (?> [^()]+ ) "... against ")" 11 <)> | 1: EXACT <(>(3) failed... BRANCH failed... whilem: failed, trying continuation... 11 <)> | 31: NOTHING(32) 11 <)> | 32: EXACT <)>(34) 12 <> | 34: EOL(35) 12 <> | 35: END(0) Match successful! Freeing REx: "%n \(%n (?:%n (?> [^()]+ ) "... Freeing REx: "\w+(?^x:%n \(%n (?:%n (?> ["...