PREG_MATCH 检查所有单词和条件 [英] PREG_MATCH check all words and condition

查看:58
本文介绍了PREG_MATCH 检查所有单词和条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个正则表达式,它在 OR 条件下搜索搜索词,这样就提供了三个单词,无论它们的顺序如何.现在我只想放置一个 AND 条件,因为我想以不同顺序在一个字符串中同时获取所有三个单词.

I've written a regular expression which seraches the search terms in OR condition, such that provided three words have in string irrespective of their order. Now i want to just put an AND condition as I want to get ALL THREE words simulatniously in a string with different order.

这是我的 preg_match() 常规表达式.

Here's my preg_match() regular expresison.

$myPregMatch = (preg_match("/\b^(Word1|Word2|Word3)\b/", "Word4 Word2 Word1 Word3 Word5 Word7"));
 if ($myPregMatch){
    echo  "FOUND !!!";
 }

我想在字符串 "Word4 Word2 Word1 Word3 Word5 Word7" 中找到所有单词都以不同的顺序排列.如果示例字符串是 "Word5 Word7 Word3 Word2" 那么它不应该返回.

I want to find in string "Word4 Word2 Word1 Word3 Word5 Word7" that all words are in it with different order. and if sample string is "Word5 Word7 Word3 Word2" then it shouldn't returns.

推荐答案

您需要锚定的前瞻:

^(?=.*\bWord1\b)(?=.*\bWord2\b)(?=.*\bWord3\b)

参见演示

如果输入字符串中有换行符,则需要使用/s修饰符.

If there are newline symbols in the input string, you need to use an /s modifier.

这是一个 IDEONE 演示:

$re = '/^(?=.*\bWord1\b)(?=.*\bWord2\b)(?=.*\bWord3\b)/'; 
$str = "Word4 Word2 Word1 Word3 Word5 Word7"; 

$myPregMatch = (preg_match($re, $str));
 if ($myPregMatch){
    echo  "FOUND !!!";
 }

结果:找到!!!

这篇关于PREG_MATCH 检查所有单词和条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆