Tcl Source Code

View Ticket
Login
2026-11-16
15:00
Fix [524b35]: a {1,1} quantifier was always dropped as vacuous check-in: e02cef20de user: jan.nijtmans tags: core-9-0-branch
2026-11-10
16:40 • Ticket [524b35a8b5] can't force regex subexpression greediness as stated by ... artifact: fdda65c176 user: oehhar
16:40 • Ticket [fe4fca32d9] mixed-greediness regular expressions need better documentati... artifact: 0437bd3e6e user: oehhar
2026-10-14
11:54
Fix [524b35]: a {1,1} quantifier was always dropped as vacuous check-in: b0b515295b user: jan.nijtmans tags: trunk, main
2026-10-10
18:18 • Ticket [524b35a8b5] can't force regex subexpression greediness as stated by ... artifact: c08fbbcf6d user: serhiy.storchaka
18:17
Fix [524b35a8b5]: a {1,1} quantifier was always dropped as vacuous, so {1,1}? could not make a greed... closed check-in: 98ef5206be user: serhiy.storchaka tags: regexp-1-1-quantifier-preference
2019-05-12
20:31 • Ticket [524b35a8b5] can't force regex subexpression greediness as stated by ... artifact: 08e0393ac0 user: jan.nijtmans
20:28
Proposed fix for bug [524b35a8b5]. No test-case yet. leaf check-in: 0040d90962 user: jan.nijtmans tags: bug-524b35a8b5
18:45 • New ticket [524b35a8b5] can't force regex subexpression greediness as stated... artifact: 98310803f7 user: tgl

Ticket UUID: 524b35a8b5f9ec643fc323582217193d91247430
Title: can't force regex subexpression greediness as stated by man page
Type: Bug Created on: 2019-05-12 18:45:58
Submitter: tgl Assigned to: nobody
Subsystem: 43. Regexp Severity: Minor
Priority: 5 Medium Last modified: 2026-11-10 16:40:59
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.18, 9.0.5, 9.1.0
Description:
The re_syntax man page states

The quantifiers \fB{1,1}\fR and \fB{1,1}?\fR can be used to
force longest and shortest preference, respectively, on a
subexpression or a whole RE.

However, this doesn't actually work reliably, for example

% regexp -inline {^(l*)(.*)(f*)$} {llmmmfff}
llmmmfff ll mmmfff {}
% regexp -inline {^(l*){1,1}?(.*)(f*)$} {llmmmfff}
llmmmfff ll mmmfff {}

The first result is fine, but the second result should have been

llmmmfff {} llmmmfff {}

that is, the (l*) should have been forced to be non-greedy.

I tracked this down to an over-optimistic optimization in parseqatom():
it throws away a {1,1} quantifier without regard to whether it's trying to impose
a different greediness than the atom would have natively.

This seems to be sufficient to fix it:

@@ -1155,7 +1155,10 @@ parseqatom(struct vars *v,
 		/* rest of branch can be strung starting from atom->end */
 		s2 = atom->end;
 	}
-	else if (m == 1 && n == 1)
+	else if (m == 1 && n == 1 &&
+			 (qprefer == 0 ||
+			  (atom->flags & (LONGER | SHORTER | MIXED)) == 0 ||
+			  qprefer == (atom->flags & (LONGER | SHORTER | MIXED))))
 	{
 		/* no/vacuous quantifier:  done */
 		EMPTYARC(s, atom->begin);	/* empty prefix */

(That's from a draft Postgres patch, your formatting of the code is different)

BTW, I think that the weasel wording in the NOTE in re_syntax.n immediately
after the above-quoted bit is due to this bug.

Background at
https://www.postgresql.org/message-id/flat/20583.1557680147%40sss.pgh.pa.us
User Comments:
jan.nijtmans added on 2019-05-12 20:31:56:
Proposed fix committed in bug-524b35a8b5 branch. No test-case yet.

serhiy.storchaka added on 2026-10-10 18:18:30:

Still the same in 8.6, 9.0 and 9.1: parseqatom() dropped every {1,1} as vacuous, so {1,1}? could not make a greedy atom non-greedy, and {1,1} could not make a non-greedy atom greedy (e.g. (.*?){1,1}(.*)), contrary to re_syntax.n.

Proposed fix in branch regexp-1-1-quantifier-preference: your patch, which keeps the quantifier when it asks for a different preference than the atom has. With it, the results agree with PostgreSQL on the example and on other {1,1} and {1} cases. Tests reg-25.4 to 25.9. Branch bug-524b35a8b5 had the same change without tests, based on core-8-5-branch.


oehhar added on 2026-11-10 16:40:59:

Jan, is there any additional action? Serhiy proposes to also merge the doc fix in [fe4fca32d].

Thanks for all, Harald