简体   繁体   中英

Python finditer() output confusion

I was trying this code as mentioned in docs.python:

>>> iterator = p.finditer('12 drummers drumming, 11 ... 10 ...')
>>> for match in iterator:
...     print (match.span())
... 
(0, 2)
(22, 24)
(29, 31)

where p was defined as

p = re.compile('\d+')

But 12,11 and 10 all are two digit numbers. So why is it showing 0-2, 22-24, 29-31? Whats the deal with this output. Whats the benefit if any?

The span maps nicely onto a slice. ie the end index is not included

>>> ['12 drummers drumming, 11 '[slice(*i)] for i in ((0,2), (22,24))]
['12', '11']

As stated in the doc too:

span() Return a tuple containing the (start, end) positions of the match

Prefer using match.group to display match content :

>>> iterator = p.finditer('12 drummers drumming, 11 ... 10 ...')
>>> for match in iterator:
    print match.group(), match.span()



12 (0, 2)
11 (22, 24)
10 (29, 31)

Looking at the documentation of span() , start() , and end() , span() returns a tuple of the start and end of the match.

For MatchObject m, return the 2-tuple (m.start(group), m.end(group)). Note that if group did not contribute to the match, this is (-1, -1). group defaults to zero, the entire match.

start() and end() are the indices that indicate where the matched substring exists.

Return the indices of the start and end of the substring matched by group; group defaults to zero (meaning the whole matched substring).

So match.span() will return just that tuple. If you want to display the actual match, try match.group() .

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