Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 764283071ccab371cdfec09d05121884aa94abafec06d57d627ad6495c4901fd
Ticket: b73f1296966202b2beeb14fec5534dec6a408012
regexp -all misses an empty match at the end of the string
User & Date: serhiy.storchaka on 2026-10-11 08:18:34
Changes

  1. cmimetype changed to: "text/x-fossil-wiki"
  2. comment changed to:
    <tt>regexp -all</tt> does not try to match at the end of the string, so an RE that can match the empty string there misses its last match. <tt>regsub -all</tt> does try there, so the two disagree:
    <verbatim>
    regexp -all {a*} ab                  ;# 2, but regsub -all {a*} ab <&> gives <a><><> (3 matches)
    regexp -all -inline -indices -line {$} "a\n"   ;# {1 0}, but the empty line at the end also matches: {1 0} {2 1}
    </verbatim>
    PostgreSQL, Python, Perl, Java, Ruby and JavaScript all report the match at the end (3 matches for the first example). Tests regexp-23.5, 23.7 and 23.9, added for [2826551fff], expect it too, but are marked knownBug.
    
    The cause is the end of the <tt>-all</tt> loop in <tt>Tcl_RegexpObjCmd()</tt>: it stops when the next offset reaches the end of the string (<tt>offset >= stringLength</tt>), while <tt>Tcl_RegsubObjCmd()</tt> loops while <tt>offset <= wlen</tt>. Fixing it changes the number of matches that <tt>regexp -all</tt> reports for such REs, and several existing tests (regexp-18.8 to 18.10, 23.2, 23.3, 26.10 to 26.13) that expect the current results.
    
  3. foundin changed to: "8.6.18, 9.0.5, 9.1.0"
  4. login: "serhiy.storchaka"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. severity changed to: "Minor"
  8. status changed to: "Open"
  9. submitter changed to: "serhiy.storchaka"
  10. subsystem changed to: "43. Regexp"
  11. title changed to:
    regexp -all misses an empty match at the end of the string
    
  12. type changed to: "Bug"