Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-16
12:22
Fix [5ea71fdcd3]: restrictions of lookahead constraints were not applied nested. Rebase to trunk leaf check-in: 055b73e260 user: jan.nijtmans tags: regexp-lacon-nested-parens
2026-10-11
08:04 • Ticket [5ea71fdcd3] Regexp fails to enforce LACON restrictions inside nested par... artifact: 7a42f695cd user: serhiy.storchaka
08:00
Fix [5ea71fdcd3]: the restrictions of lookahead constraints (no backrefs, no capturing parentheses) ... check-in: bac9120832 user: serhiy.storchaka tags: regexp-lacon-nested-parens
2015-11-07
18:31 • Add attachment laconfix.patch to ticket [5ea71fdcd3] artifact: 19036eb915 user: tgl
18:31 • New ticket [5ea71fdcd3] Regexp fails to enforce LACON restrictions inside nested... artifact: 563771520a user: tgl

Regexp fails to enforce LACON restrictions inside nested parentheses

Open5ea71fdcd3291c38dff80d6ccdaf30f4969316e4Created 11 years agoUpdated 53 days ago
Descriptionplain2015-11-07 18:31:03
Per the manual, backrefs are not allowed inside lookahead constraints, and all parentheses therein are considered non-capturing.  But the regexp parser forgets these restrictions when it recurses to enter a parenthesized subexpression inside a LACON.  So we get silliness like this (tested with 8.6.4):

% regexp -inline {x(\w)(?=(\1))} {xyz}
xy y
% regexp -inline {x(?=((foo)))} {xfoobar}
x {}

Fix is a one-liner, see attached patch, which is a Tcl-ified version of
http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=a43b4ab1111ca5e5f40a2ddd8e56bf999b9fdad9
Referenced by2
  • 055b73e260Fix [5ea71fdcd3]: restrictions of lookahead constraints were not applied nested. Rebase to trunk check-in2026-10-16 12:22:41
  • bac9120832Fix [5ea71fdcd3]: the restrictions of lookahead constraints (no backrefs, no capturing parentheses) were not applied to parentheses nested inside them. Pass the context type when parsing the nested parentheses. Patch by Tom Lane. New tests reg-6.21 to 6.24. check-in2026-10-11 08:00:23
serhiy.storchakawiki2026-10-11 08:04:06

Still the same in 8.6, 9.0 and 9.1.

Proposed fix in branch regexp-lacon-nested-parens: your patch. Both examples now behave as in PostgreSQL: the first one gives "invalid backreference number", the second one matches x without submatches. Tests reg-6.21 to 6.24.

This is a breaking change. Parentheses nested in a lookahead constraint no longer count as capturing, so the following subexpressions are renumbered, and REs with a backref inside a lookahead constraint no longer compile. Both are what re_syntax.n has always documented, but existing code may rely on the old behaviour. Code that used non-capturing groups (?:...) inside lookahead constraints, the workaround for this bug, is not affected.

Log in (anonymous is fine) to add a comment.