[docs] [issue7940] re.finditer and re.findall should support negative end positions

Ezio Melotti report at bugs.python.org
Sun Jul 14 10:01:58 EDT 2019


Ezio Melotti <ezio.melotti at gmail.com> added the comment:

Sorry, I was wrong.  re.findall accepts negative indices for both start and end but they silently get converted to 0, which is arguably an unexpected behavior.

This is an example of the current behavior:
>>> s, e = 1, 4; re.compile('.').findall('abcde', s, e), 'abcde'[s:e]
(['b', 'c', 'd'], 'bcd')
>>> s, e = -4, 4; re.compile('.').findall('abcde', s, e), 'abcde'[s:e]
(['a', 'b', 'c', 'd'], 'bcd')
>>> s, e = 1, -1; re.compile('.').findall('abcde', s, e), 'abcde'[s:e]
([], 'bcd')
>>> s, e = -4, -1; re.compile('.').findall('abcde', s, e), 'abcde'[s:e]
([], 'bcd')

With the patch, all these return ['b', 'c', 'd'].  This change might indeed cause issues because it's a change in behavior, but I'm also not sure there are many cases where one would want a negative index to be treated as 0.  Maybe we could raise a FutureWarning in the next release and change the behavior afterwards?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue7940>
_______________________________________


More information about the docs mailing list