Issue 33368: Inaccuracy in https://docs.python.org/3/library/re.html#re.match.end (original) (raw)
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.
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.