dongqianchi0512 2019-01-10 22:43
浏览 78
已采纳

仅当两个以上的分组匹配时,如何使正则表达式匹配?

How do I make quantifier only match if at least 2 of the grouped words are found?

I need this to match: ((?i:\bjack\b)|(?i:\bjill\b)|(?i:\bjohn\b)){2,}

https://i.imgur.com/sM2ZhW9.png

And I need this to not match:

https://i.imgur.com/V5eHOYi.png

Match if >= 2 of the words are found, in any order and case

How do I go about doing that? After a few hrs I'm tired of reading regex. Thanks!

  • 写回答

2条回答 默认 最新

  • duangutian1426 2019-01-11 05:29
    关注

    You could do:

    re, _ := regexp.Compile(`\b(?i:jack|jill|john)\b`)
    ma := re.FindAllString("Jill is friends with John. But Jack doesn't know.", -1)
    if len(ma) < 2 //...then there aren't enough matches.
    

    Alternatively, (\b(?i:jack|jill|john)\b.*){2,} does what you want, I think.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启