fatesses
fatesses
采纳率98%
2019-09-11 19:29

python 正则表达式 匹配指定单词前几个单词

10
已采纳
import re
text = "May we stop to remember the 2,996 people that died & the more than 6000 injured on #September11"

m = re.findall(r"\bdied|injured\b", text)

if m:

    print(m)

else:

    print('not match')

我 想匹配的是died和injured包括他们前面3个单词,包括数字,主要是需要数字,这个该如何填写呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • caozhy 从今以后生命中的每一秒都属于我爱的人 2年前
    # -*- coding: UTF-8 -*-
    import re
    text = "May we stop to remember the 2,996 people that died & the more than 6000 injured on #September11"
    
    m = re.findall("(([a-zA-Z\\d\\,]+\\s){3})(died|injured)", text)
    
    if m:
        for i in range(0, len(m)):
            print(m[i][2] + "--->" + m[i][0])
    else:
        print('not match')
    

    died--->2,996 people that
    injured--->more than 6000

    点赞 2 评论 复制链接分享
  • dabocaiqq dabocaiqq 2年前
  • qq_39412061 吃鸡王者 2年前

    用list实现也很直观:
    text = "May we stop to remember the 2,996 people that died & the more than 6000 injured on September11"
    text=text.split()
    print(text[text.index('died')-3:text.index('died')+1])
    print(text[text.index('injured')-3:text.index('injured')+1])

    点赞 评论 复制链接分享
  • qq_23854777 qq_23854777 2年前

    .*([,\b]*).+([,\b]*).* 试试

    每个括号是一个子表达式 在你 匹配的对象中应该有个成员 可以得到 子表示前面或者 后面的值

    括号中 的子表达式 就是 数字的值 你只需要在匹配后 得到子表达式 匹配的结果 就可以得到 数字了
    没用过 puthon

    点赞 评论 复制链接分享

相关推荐