正则表达式为两个或多个单词php

时间:2015-06-26 14:26:59

标签: php regex

我的职位名称如下:

Reactive Customer Coach
Customer Reactive Coach
Technical Reactive Customer Coach
Field Engineer
Customer Engineer for FTTC

我想要匹配:

Reactive Coach(字符串中出现Reactive关键字或Coach关键字无关紧要) 还希望匹配Engineer关键字(同样可以出现在字符串中的任何位置)

如果找不到这些关键字,则应返回FALSE

对于上述场景,什么是合适的正则表达式? (我是正则表达式的新手,所以我自己还没有尝试过任何东西)

3 个答案:

答案 0 :(得分:1)

你可以在PHP中尝试这个正则表达式:

(?|(\bReactive\b).*?(\bCoach\b(?! *OM\b))|(\bCoach\b).*?(\bReactive\b)|(\bEngineer\b))

RegEx Demo

(?!...)非捕获组。在此构造的每个替代项中声明的子模式将从相同的索引开始。

答案 1 :(得分:0)

对于简单匹配,regexp不是必需的

<?php
/**
*/
error_reporting(E_ALL);
ini_set('display_errors',1);
$in = [
 'Reactive Customer Coach'
,'Customer Reactive Coach'
,'Technical Reactive Customer Coach'
,'Field Engineer'
,'Customer Engineer for FTTC'
,'No such as'
];

function x($s) {
    if ( false !== mb_strpos($s,'Reactive') ) {
        return false !== mb_strpos($s,'Coach');
    }
    return false !== mb_strpos($s,'Engineer');
}

foreach ($in as $i) {
    echo $i,' ',x($i)?'Match':'',"\n";
}

答案 2 :(得分:0)

你可以使用strpos,不区分大小写(Reactive | reactive | REACTIVE)stripos

if(stripos($mystring, 'reactive') != FALSE && stripos($mystring, 'coach') != FALSE){
    //string contains reactive or Coach
} else if(stripos($mystring, 'engineer') != FALSE){
    //string contains Engineer
} else{
    return FALSE;
}