Tcl Source Code

View Ticket
Login
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-11-08
20:52 • Ticket [fe4fca32d9]: 3 changes artifact: 9564d24721 user: dkf
2026-10-11
10:51 • Ticket [219219ffff] greedy vs. non-greedy confusion status still Open with 3... artifact: 31540dc69b user: serhiy.storchaka
10:49 • Ticket [fe4fca32d9] mixed-greediness regular expressions need better documentati... artifact: c30cd066c6 user: serhiy.storchaka
10:46
Document how the first quantifier sets the preference of the whole RE ([fe4fca32d9]). Explain that ... leaf check-in: 1512c7ee75 user: serhiy.storchaka tags: doc-re-syntax-preference
2015-03-24
14:55 • New ticket [fe4fca32d9] mixed-greediness regular expressions need better documen... artifact: 3b22e98da4 user: glennj

Ticket UUID: fe4fca32d988f67704a9fec3372bf340c81c3ce
Title: mixed-greediness regular expressions need better documentation
Type: Bug Created on: 2015-03-24 14:55:43
Submitter: glennj Assigned to: nobody
Subsystem: 43. Regexp Severity: Minor
Priority: 5 Medium Last modified: 2026-11-10 16:40:29
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6
Description:
Currently, the MATCHING section of the re_syntax man page talks about "preferences", and states:

> A branch has the same preference as the first quantified atom in it which has a preference.

I don't this this is sufficiently clear to illustrate the differences between

    $ perl -e 'if ("1234" =~ /(\d+?)(\d+)/) {print "$& $1 $2\n"}'
    1234 1 234
    $ echo 'puts [regexp -inline {(\d+?)(\d+)} "1234"]' | tclsh
    12 1 2

Can we have an explicit statement? Something like:

> If the first quantifier in a branch of a RE is non-greedy, /all/ quantifiers in the branch will be considered as non-greedy.

However, I just noticed something I can't explain: what's the difference between these?

    $ echo 'puts [regexp -inline {(?:(\d+?)(\d+))} "1234"]' | tclsh
    12 1 2
    $ echo 'puts [regexp -inline {s+?|(?:(\d+?)(\d+))} "1234"]' | tclsh
    1234 1 234
User Comments:
serhiy.storchaka added on 2026-10-11 10:49:37:

Proposed docs fix in branch doc-re-syntax-preference. It replaces the vague NOTE in re_syntax(n) MATCHING. The new text explains that the first quantified atom (or a top-level |) fixes only the length of the whole match, and that each quantifier keeps its own preference within that match. It covers your s+?|(?:(\d+?)(\d+)) example, notes that this differs from Perl, Python and other engines, and shows how to work around it.


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

Serhiy proposed to merge this with ticket [524b35a8b5].