Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a14949605bf2abcab8441a3cc8e4327e5f1503ef6c3ca2eb5f66a99124536313
Ticket: 219219ffffffffffffffffffffffffffffffffff
greedy vs. non-greedy confusion
User & Date: dram on 2017-10-26 01:40:45
Changes

  1. assignee changed to: "aku"
  2. closer changed to: "nobody"
  3. comment changed to:
    OriginalBugID: 4001 Bug
    Version: 8.2.3
    SubmitDate: '2000-01-10'
    LastModified: '2000-01-27'
    Severity: MED
    Status: Assigned
    Submitter: techsupp
    ChangedBy: hobbs
    RelatedBugIDs: 2866
    OS: BSD
    OSVersion: NetBSD-1.4.1/i386
    FixedDate: '2000-10-25'
    ClosedDate: '2000-10-25'
    
    
    Name: hume smith
    
    ReproducibleScript:
    > tclsh
    % regexp {x.*?([a-z]+)} {1234x56789word101112} a b
    1
    % set a
    x56789w
    % set b
    w
    % set tcl_patchLevel
    8.2.3
    %
    
    ObservedBehavior:
    the + was matched in a nongreedy fashion; shouldn't it be greedy?
    
    DesiredBehavior:
    % set a
    x56789word
    % set b
    word
    %
    
    Patch:
    
    
    PatchFiles:
    
    
    
    
    Henry Spencer has noted some problems with mixing greedy and
    non-greedy quantifiers in the new regexp code.  He's cc'ed on
    this, but in the meantime, the work-around is:
        regexp {x[^a-z]*([a-z]+)} {1234x56789word101112} a b 
    -- 01/10/2000 hobbs
    From HS:
    
    This is the same old problem:  people accustomed to Perl are not grasping
    the idea that the whole RE is greedy or non-greedy, but *not* some mixture
    of the two.  In this case, it is non-greedy since the first thing in it
    which cares is non-greedy.  The + is being as greedy as it can, within the
    constraints set by the behavior of the whole RE. 
    
    In short, the behavior, while surprising, is as documented.  It's not an
    outright bug; it may, however, be a misfeature.
     
    -- 01/10/2000 hobbs
    
  4. icomment:
    Also encoutered this problem recently, with a simpler case:
    
      % regexp -inline {(.+?), (.+)} "foo, bar"
      {foo, b} foo b
    
    But this is expected behaviour as stated by re_syntax(n), and PostgreSQL have a more throughly description[1].
    
    
    [1] https://www.postgresql.org/docs/10/static/functions-matching.html#functions-posix-regexp  (in 9.7.3.5. Regular Expression Matching Rules)
    
  5. login: "dram"
  6. mimetype: "text/plain"
  7. severity changed to: "Minor"