classification
Title: Inaccuracy in https://docs.python.org/3/library/re.html#re.match.end
Type: enhancement Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: C. Y. Hollander, docs@python, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-04-26 19:08 by C. Y. Hollander, last changed 2018-04-26 20:03 by serhiy.storchaka. This issue is now closed.

Messages (2)
msg315813 - (view) Author: C. Y. Hollander (C. Y. Hollander) Date: 2018-04-26 19:08
The documentation states that match.end([group]) returns "the ind[ex] of the... end of the substring matched by group". In fact, it returns [said index] + 1, as demonstrated by the example below:

s = 'example'
sre = re.search('le', s)
s[sre.end()]

Incidentally, I don't see the logic of this behaviour, but in any case it should be correctly documented.
msg315816 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-26 20:03
It returns not the index of the last character of the substring, but the index of the end of the substring, i.e. the position past the last character of the substring.

Try s[:sre.end()] and s[sre.end():].

s[sre.begin()] gives you the part of s before the matched substring, s[sre.begin():sre.end()] gives you the matched substring itself (the same as sre.group()) and s[sre.end():] gives you the part of s after the matched substring.
History
Date User Action Args
2018-04-26 20:03:03serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg315816

resolution: not a bug
stage: resolved
2018-04-26 19:08:57C. Y. Hollandercreate