简体   繁体   中英

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?

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM