加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 11:27:17 所属栏目: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的所有出现,然后返回以下内
我必须在大字符串中的子字符串匹配之前和之后提取两个单词.例如:

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*nameW*w*W*w*'
for i in re.findall(sub,re.I):
    i=i.strip(" .")
    print i

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读