str.find() returns the beginning offset of a match in a string.
How can one get the end offset?
I know that one way is to add it to the length of the match. But is there a way to get it directly?
e.g.
>>> a = 'Lorem ipsum dolor sit amet'
>>> a.find('ipsum')
6
What I want is:
>>> a.find('ipsum')+len('ipsum')
11
This is trivial in case of str.find(). But gets more important in case of regex, since the length of the matched expression is not known beforehand. 
The objects returned by the search and match functions in Python's re module have a span() method that returns the start and end positions of the matched regular expression:
>>> import re
>>> a = 'Lorem ipsum dolor sit amet'
>>> re.search('ipsum', a).span()
(6, 11)
>>> re.search('sum.*sit', a).span()
(8, 21)
The start and end positions can also be returned individually with .start() and .end().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With