Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the end offset of a matched string or regex

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.

like image 548
CentAu Avatar asked Mar 12 '15 21:03

CentAu


1 Answers

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().

like image 93
Alex Riley Avatar answered Nov 19 '22 09:11

Alex Riley