正则表达式:匹配至少两个搜索词

use*_*228 3 regex search

我有一个搜索词列表,我希望有一个正则表达式匹配所有至少有两个项目的项目.

条款:战争|军队|战斗|反叛者|冲突

比赛:反叛分子军队之间的战争导致本周发生了几次冲突.(4次点击)

不匹配:在战争的恐怖,奥巴马政府希望增加无人机袭击的数量.(仅1次点击)

背景:我使用微小的rss来收集和过滤新闻报道项目的大量信息.我每天获得1000 - 2000个Feed项,并希望按关键字过滤它们.通过使用| OR表达式,我得到了很多误报,所以我想我可以在一个feed项中要求两个匹配.

谢谢!

编辑:

我对正则表达式知之甚少,所以到目前为止我一直坚持使用简单的| OR运算符.我尝试将搜索词放在括号中(war | fight | etc){2,},但只有在项目使用相同的词两次时才匹配.

EDIT2:对不起,我是正则表达式之类的新手.事实是:正则表达式查询mysql数据库.它作为过滤器输入到tt-rss后端,它只允许一行(虽然理论上是无限数量的字符).在将feed项导入mysql数据库时使用过滤器.

bee*_*jay 7

(.*?\b(war|army|fighting|rebels|clashes)\b){2,}
Run Code Online (Sandbox Code Playgroud)

如果您需要避免匹配相同的术语,您可以使用:

.*?\b(war|army|fighting|rebels|clashes).*?(\b(?!\1)(war|army|fighting|rebels|clashes)\b)
Run Code Online (Sandbox Code Playgroud)

它匹配一个术语,但通过使用负前瞻避免再次匹配相同的术语.

在java中:

Pattern multiword = Pattern.compile(
    ".*?(\\b(war|army|fighting|rebels|clashes)\\b)" +
    ".*?(\\b(?!\\1)(war|army|fighting|rebels|clashes)\\b)"
);
Matcher m;
for(String str : Arrays.asList(
        "war",
        "war war war",
        "warm farmy people",
        "In the war on terror rebels eating faces"

)) {
    m = multiword.matcher(str);
    if(m.find()) {
        logger.info(str + " : " + m.group(0));
    } else {
        logger.info(str + " : no match.");
    }
}
Run Code Online (Sandbox Code Playgroud)

打印:

war : no match.
war war war : no match.
warm farmy people : no match.
In the war on terror rebels eating faces : In the war on terror rebels
Run Code Online (Sandbox Code Playgroud)