Python匹配对象与索引使用

>>> s='''Lisf can be dreams,
...Lisfe can be great thoughts;
...Life can mean a person
...Sitting in a count.'''

 r=re.compile("\\b(?P\w+)a(\w+)\\b")


>>> m=r.search(s)

>>> m
<_sre.SRE_Match object; span=(5, 8), match='can'>
>>> m.start()
5
>>> m=r.search(s,9)
>>> m.start()
12
>>> m.start(1)
12
>>> m.start(2)
16
>>> m.end(1)
15
>>> m.end()
18
>>> m.span()
(12, 18)
>>> s[12:18]
'dreams'
>>> m.span(2)
(16, 18)

你可能感兴趣的:(Python高级编程)