Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2015-05-19
17:02 • Ticket [11250a236d] unexpected non greedy matching behaviour status still Cl... artifact: 02e9d5f52a user: ferrieux
16:50 • Ticket [11250a236d]: 5 changes artifact: a88e4107e1 user: aku
10:57 • Ticket [11250a236d]: 5 changes artifact: 778097dfc3 user: anonymous
2015-05-18
08:24 • Ticket [11250a236d]: 4 changes artifact: a763584697 user: dkf
08:22 • Closed ticket [11250a236d]. artifact: 40864f6ffc user: dkf
08:20
[11250a236d] Made the documentation of non-greediness overrides more obvious. check-in: 93531a5a13 user: dkf tags: trunk
2015-05-17
18:38 • Ticket [11250a236d] unexpected non greedy matching behaviour status still Op... artifact: 410b2a9902 user: anonymous
15:41 • Ticket [11250a236d]: 4 changes artifact: c4c0594daf user: ferrieux
14:55 • New ticket [11250a236d]. artifact: 26238b7f75 user: anonymous

unexpected non greedy matching behaviour

Closed / Fixed11250a236da29e6badf7755a1b67995defa5170fCreated 12 years agoUpdated 12 years ago
Descriptionplain2015-05-17 14:55:33
since there seems no suitable mailing list to ask and I presume it's a bug I'm reporting this here: non-greedy matching seems not to work as it should in certain contexts, specifically, matching seemingly becomes greedy if the respective atom is preceded by a pattern containing `+'  to indicate arbitrary (one or more) occurrence of chars from a range or class.

example:

# a string with four sub-patterns I want to capture (the latex-like constructs `\emph{xxx}'):

set s "\emph{A} a \emph{B} b \emph{C} c \ital{D} d"

# this one correctly finds all four of them, when specifying that the stuff before `{xxx}' consists of exactly 4 letters:

regexp -all  {(\\[a-z]{4})\{(.*?)\}} $s dum; puts $dum; # ==> \ital{D}  <== (OK)

# allowing "one or more" chars instead of "exactly 4" changes the match to "greedy", i.e. it reports a _single_ match of
# everyting up to the last closing `}':

regexp -all  {(\\[a-z]+)\{(.*?)\}} $s dum; puts $dum; # == > \emph{A} a \emph{B} b \emph{C} c \ital{D} <==(WRONG)

# the same problem here:
regexp -all  {(\\w+)\{(.*?)\}} $s dum; puts $dum; # == > \emph{A} a \emph{B} b \emph{C} c \ital{D} <==(WRONG)


is this a bug or am I completely misunderstanding how non-greedy matching is supposed to work/be used?

thanks 

joerg
Referenced by1
  • 93531a5a13[11250a236d] Made the documentation of non-greediness overrides more obvious. check-in2015-05-18 08:20:43
ferrieuxplain2015-05-17 15:41:42
As it turns out, Tcl regexps don't allow for mixed-greediness:

http://stackoverflow.com/questions/11771610/tcl-regular-expression-help-needed-on-non-greedy-quantifiers-in-pattern-with

I admit that this fact could be made more conspicuous in the re_syntax manpage though.

Donal, care for a doc fix ?
anonymousplain2015-05-17 18:38:35
I see now after looking long and hard at `man re_syntax' that it somehow _is_ explained but making this more obvious would be good. it seems a solution in my case is to to use

regexp -all  {\\(?:\w+){1,1}?{(.*?)}} $s dum

this "trick" of appending `{1,1}?' to the greedy part in order to make it non-greedy overall (if it _is_ the way to solve this, might especially deserve to be pointed out more pronouncedly.
dkfhtml2015-05-18 08:22:27

Done, but I don't think it helps as much as people really want; the real problem is that it is a long page with a lot of content on it. I don't know what to do about that without making the problem worse.

dkfhtml2015-05-18 08:24:54

Added text (post-render):

              NOTE:  This  means  that you can usually make a RE be non‐greedy
              overall by putting {1,1}? after one of the first  non‐constraint
              atoms or parenthesized sub‐expressions in it. It pays to experi‐
              ment with the placing of this non‐greediness override on a suit‐
              able  range  of input texts when you are writing a RE if you are
              using this level of complexity.

              For example, this regular expression  is  non‐greedy,  and  will
              match  the  shortest substring possible given that “abc” will be
              matched as early as possible (the  quantifier  does  not  change
              that):

                     ab{1,1}?c.*x.*cba

              The  atom  “a”  has no greediness preference, we explicitly give
              one for “b”, and the remaining quantifiers are overridden to  be
              non‐greedy by the preceding non‐greedy quantifier.

anonymousplain2015-05-19 10:57:55
short feedback: I like the change to the manpage and believe it is helpful. in absence of another "communication channel" and  if someone from core is reading this: I am very new to tcl and am not able to find a suitable tcl-users mailing list. isn't there any? I would not have reported my regexp problem as a bug in the first place if I would have found a means to ask the community otherwise. so, what do you recommend? it seems wrong to flood the bug tracker with stuff like this inquiry ...

thx/joerg
akuplain2015-05-19 16:50:27
Top of the <a href="http://core.tcl.tk/">core</a> website lists a number of related websites and channels, like comp.lang.tcl.
ferrieuxplain2015-05-19 17:02:17
The recommended, active community meeting point is the comp.lang.tcl newsgroup.
Log in (anonymous is fine) to add a comment.