这是我的代码:
>>> text= """ this is an example Item 2 text text <B>Item 2. example"""
>>> a=re.search ('(?<=<B>)Item 2\.',text)
>>> b = a.span()
>>> print (b)
(45, 57)
>>>
如何打印第一个索引编号(45之前)之前的所有文本?
答案 0 :(得分:2)
使用text[:start]
:
In [76]: import re
In [77]: text = """ this is an example Item 2 text text <B>Item 2. example"""
In [78]: a = re.search ('(?<=<B>)Item 2\.',text)
In [79]: start, end = a.span()
In [80]: text[:start]
Out[80]: ' this is an example Item 2 text text <B>'
匹配对象a
也知道text
的值;它可以通过string
属性访问:
In [91]: a.string[:start]
Out[91]: ' this is an example Item 2 text text <B>'