Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-13
12:17 • Closed ticket [b73f129696]: regexp -all misses an empty match at the end of the ... artifact: d30d5562ff user: jan.nijtmans
2010-08-12
15:56 • Closed ticket [2826551fff]: Line-sensitivity at -start, but also -all plus 7... artifact: 0535067d9e user: dkf
04:20 • Ticket [2826551fff]: 4 changes artifact: 9e4bffb7f1 user: lars_h
2010-08-10
00:54 • Ticket [2826551fff]: 1 change artifact: 8bafbc1947 user: dgp
2010-08-09
23:23 • Ticket [2826551fff]: 4 changes artifact: fdd0a1e2bf user: dgp
2010-02-11
18:15 • Ticket [2826551fff]: 4 changes artifact: 278f3026ba user: dkf
2010-02-09
18:28 • Ticket [2826551fff]: 4 changes artifact: b0a888e561 user: mdejong
2009-10-16
05:25 • Ticket [2826551fff]: 4 changes artifact: ca87afa673 user: jkock
2009-07-24
12:53 • New ticket [2826551fff]. artifact: 0ad9565c2a user: lars_h

Line-sensitivity at -start, but also -all

Closed2826551fffffffffffffffffffffffffffffffffCreated 17 years agoUpdated 16 years ago
Descriptionplain2009-07-24 12:53:45
I was going to use [regexp -all -inline] to split a unified diff into chunks, 
but found that this would skip every second chunk. This seems to be 
related to ^ not matching the beginning of line if the newline is before 
the -start position.

SMALL EXAMPLE:

set data {
@1
 2
+3
@4
-5
+6
 7
@8
 9
}
regexp -all -inline {(?n)^@.*\n(?:[^@].*\n?)*} $data

RETURNS

{@1
 2
+3
} {@8
 9
}

RATHER THAN

{@1
 2
+3
} {@4
-5
+6
 7
} {@8
 9
}

EXPLANATION: A diff chunk consists of one header line beginning with @, followed by a body 
consisting of lines not beginning with @ (but rather space, plus, or minus). The header line
is thus in line-sensitive mode matched by ^@.* and a body line is matched by ^[^@].*, so 
a sensible combined regexp is (?n)^@.*\n(?:[^@].*\n?)*  Each chunk is indeed matched by
this, but when -all is used, only every second chunk is found.

In this case, dropping the ^ from the regexp allows all chunks to be found:

% regexp -all -inline {(?n)@.*\n(?:[^@].*\n?)*} $data
{@1
 2
+3
} {@4
-5
+6
 7
} {@8
 9
}

This suggests the problem may be that ^ fails to match when at the -start position, as one obvious 
way to implement [regexp -all] is as the equivalent of a loop around [regexp -start]. Indeed,

% regexp -start 1 -indices {(?n)^a} "\nab" match
0
% regexp -start 0 -indices {(?n)^a} "\nab" match
1
% set match
1 1
Referenced by1
  • d30d5562ffClosed ticket [b73f129696]: regexp -all misses an empty match at the end of the string plus 8 other changes ticket2026-10-13 12:17:27
jkockplain2009-10-16 05:25:56
Here is another description of the bug, and a contrast:

In regexp with the -all option, it is clear that the matches
must not overlap.  But how is the rule if the expression
starts with a constraint (width zero), like for example ^
or \m ?

To my surprise you cannot find a linestart after a
matched \n:

% regexp -all -inline -indices {(?n)^a\n} "a\na\na\n"
{0 1} {4 5}

The behaviour I expected is the one seen with \m instead of ^:
you _can_ find a wordstart after a matched \n:

% regexp -all -inline -indices {(?n)\ma\n} "a\na\na\n"
{0 1} {2 3} {4 5}

So somehow the regexp engine is allowed to look back to
see that a \n preceded and hence constitutes a word boundary,
but it is not allowed to look back and detect that this same
\n also constitutes a line boundary...
mdejongplain2010-02-09 18:28:48
Added patch 2948425 to addres this issue.
dkfplain2010-02-11 18:15:48
That patch now applied.
dgpplain2010-08-09 23:23:45
status?  This bug is/was 8.6 branch only?
Or is there still a need to fix a bug for new 8.5.* releases too?
dgp Ticket [2826551fff]: 1 change [History diff]2010-08-10 00:54:31
lars_hplain2010-08-12 04:20:26
To answer dgp's question: the bug was originally encountered under 8.5 (possibly even 8.4), so a fix there would be nice too. Given how little tends to change in the regexp engine, I would be surprised if fixing 8.5 is different from fixing 8.6.
dkfplain2010-08-12 15:56:35
allow_comments - 1

Backported patch to 8.5.
Log in (anonymous is fine) to add a comment.