Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2000-10-26
08:20 • Closed ticket [218370ffff]: regexp -start, -lineanchor don't combine well</i... artifact: ad7fa2f06d user: welch
00:56 • New ticket [218370ffff]. artifact: 1ab685968c user: nobody

regexp -start, -lineanchor don't combine well

Closed / Fixed218370ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:56:47
OriginalBugID: 4233 RFE
Version: 8.3
SubmitDate: '2000-02-23'
LastModified: '2000-02-23'
Severity: MED
Status: Closed
Submitter: techsupp
ChangedBy: hobbs
OS: All
FixedDate: '2000-10-25'
ClosedDate: '2000-02-23'


Name:
Vince Darley

ReproducibleScript:
This is correct:

% regexp -start 1 -lineanchor {^[a-z]*} "a\naaa" found
1
% set found
aaa

However if we instead use '-start 2', the regexp fails:

% regexp -start 2 -lineanchor {^[a-z]*} "a\naaa" found
0

even though position 2 is in fact a suitable position for ^ to match as a lineanchor.  The problem is that '-start' is implemented in too simplistic a way (and we could implement a similar '-end' with similar problems with $) -- it just passes 'string range $text $start end' to the regexp code, and turns off the ability of '^' to match the beginning of string.

The best solution is to add proper support for -start (and -end IMHO) to Henry Spencer's regexp code.

For a general workaround, consider: given a generic $pat, and a generic $text, if we wish to know the result of:

regexp -start 2 -lineanchor -- $pat $text result

Then possible workarounds need to examine whether 'string index $text 1' is a newline or not.  If it is not a newline, then I think everything should work ok.  If it is a newline, I think the best solution is to use: 'regexp -lineanchor -- $pat [string range $text 2 end] instead'.  I'm not sure whether that actually covers all possible problems...

So perhaps: 

if {$start > 0 && [regexp {[\r\n]} [string index $text $start]]} {
    regexp -lineanchor -- $pat [string range $text $start end] result
} else {
    regexp -start $start -lineanchor -- $pat $text result
}

Of course if we actually want '-indices' we then have to adjust the indices in the results appropriately by the offset we are using...   So a general workaround is kind of a pain!

Conclusion: perhaps this should be an RFE for a more complete implementation of -start (and -end).  An alternative fix would be for Tcl_RegexpObjCmd to check whether the character before the -start is a newline \r\n or not, and if it _is_, then don't turn off the ability of '^' to match the beginning of the string!

I assume 'regsub' suffers from similar problems.



The regexp -start doesn't do an implicit string range, it uses the
facilities that HS provided for just such a feature.  The effect on
'^' is documented, and due to limitations in the exposed interface. 
-- 02/23/2000 hobbs
welch Closed ticket [218370ffff]. [History diff]2000-10-26 08:20:09
Log in (anonymous is fine) to add a comment.