Python : AttributeError: 'NoneType' object has no attribute 'start'

Python : AttributeError: 'NoneType' object has no attribute 'start'

以下代码属于 NLTK 正则表达式:

import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize

scene = "Hello how! how are you? what is your problem. Can I solve with 00code for you/ by the way bye. Take care"

match_index = print(re.search("you",scene))
print(match_index.start(),match_index.end())

我得到的错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-54-5e13e5437c3e> in <module>()
----> 1 print(match_index.start(),match_index.end())

AttributeError: 'NoneType' object has no attribute 'start'

我已经包含了它的库,但它仍然显示错误。什么 我有什么方法可以处理这个错误?

match_index = print(re.search("you",scene))

printreturnsNone,所以在这一行之后,match_index就是None.

尝试在不同的行上分配和打印。

match_index = re.search("you",scene)
print(match_index)

结果:

<_sre.SRE_Match object; span=(19, 22), match='you'>
19 22