如何使用python识别字符串中单词之后的哪个分隔符



我有一个字符串:

The exam is for testing your skills. The exam includes the following:
1) Aptitude
2)synonyms
3)Reasoning

所以我使用以下代码使用字符串方法来识别单词的索引:

string.find('exam')

它给了我字符串中单词的索引。在这里,我必须确定每个句子末尾存在哪个分隔符。 例如:

The exam is for testing your skills. [here it is '.']
The exam includes the following: [here it is ':']

那么,如何根据单词搜索来识别句子结尾的词呢?

你的问题陈述有些模糊,因为子句可以用",",":",";"结尾,但可能不会结束句子。 要修改此问题,请确定要查找的标点符号并将其设置为列表。

以下代码标识所有关键字的起始位置。 然后,它会找到您认为是"从句/句子结尾"的已识别标点符号之一的第一个实例并返回它。

import re
text = '''
The exam is for testing your skills. The exam includes the following:
1) Aptitude
2)synonyms
3)Reasoning'''
targets =[m.start() for m in re.finditer('exam', text)]
end_punct = ['!','.','?',':',';']
for target in targets:
subtext = text[target:]
print(subtext)
for char in subtext:
if char in end_punct:
print(char)
break

样品返回:

#Returns:
.
:

相关内容

最新更新