python – 查找子字符串周围的单词

我必须在大字符串中的子字符串匹配之前和之后提取两个单词.例如:

sub = 'name'

str = '''My name is Avi. Name identifies who you are. It is important to have a name starting with the letter A.'''

现在我必须在str中找到sub的所有出现,然后返回以下内容:

(我的名字是Avi),(姓名标识谁),(名称以)开头

请注意,如果re是字符串后的句号,则只返回字符串之前的单词,如上例所示.

我试过了什么?

>>> import re
>>> text = '''My name is Avi. Name identifies who you are. It is important to have a name starting with the letter A.'''
>>> for m in re.finditer( 'name', text ):
...     print( 'name found', m.start(), m.end() )

这给了我匹配的子串的起始位置和结束位置.我无法进一步了解如何找到它周围的单词.

最佳答案

import re
sub = '(\w*)\W*(\w*)\W*(name)\W*(\w*)\W*(\w*)'
str1 = '''My name is Avi. Name identifies who you are. It is important to have a name starting with the letter A.'''
for i in re.findall(sub, str1, re.I):
    print " ".join([x for x in i if x != ""])

产量

My name is Avi
Name identifies who
have a name starting with

要么,

sub = '\w*\W*\w*\W*name\W*\w*\W*\w*'
for i in re.findall(sub, str1, re.I):
    i=i.strip(" .")
    print i
点赞