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 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥100 网页游戏在本地页面修改游戏结果
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead