首页 > 解决方案 > 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,}

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!

标签: regexgo

解决方案


你可以这样做:

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,}做你想做的事,我想。


推荐阅读