regex - How to make regex only match when more than two groupings match?
问题描述
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,}
And I need this to not match:
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!
解决方案
你可以这样做:
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.
或者,(\b(?i:jack|jill|john)\b.*){2,}
做你想做的事,我想。
推荐阅读
- android - 通用应用链接和多个调试 SHA256 指纹
- sql - 限制 SQL 查询结果中的重复行数
- webhooks - 创建具有范围 = 项目 id 但适用于顶级文件夹的 webhook 时出错
- node.js - 使用 spawn 方法从电子应用程序运行子进程的问题
- c++ - 如果函数使用 OPENCV Mat 作为输入,如何编写头文件?
- javascript - 如何使用 vanilla JS 在第二个元素上添加/删除类?
- c++ - 优化使用 C++ 在 SQLite DB 中查询超过 5000 万条数据记录的方式
- java - 如何获取多个Edit Text addview android的值并将其发送到MySQL
- laravel - 尝试在mapWithKeys回调中添加条件时,laravel 5.6“为foreach()提供的参数无效”
- ruby - 为什么“真或真与假”似乎同时是真假?