简体   繁体   English

两个或两个以上单词的正则表达式php

[英]Regular expressions for two or more words php

I have job titles like: 我有以下职位:

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

I would like to match: 我想搭配:

Reactive Coach (Doesn't matter where Reactive keyword or Coach keyword occurs in the string) Also would like to match Engineer keyword (again it can occur anywhere in the string) Reactive Coach (与Reactive关键字或Coach关键字在字符串中的位置无关)也希望与Engineer关键字匹配(同样可以在字符串中的任何位置出现)

It should return FALSE if these keywords are not found. 如果找不到这些关键字,则应返回FALSE

What would be a suitable regular expression for the above scenarios? 对于上述情况,什么是合适的正则表达式? (I am new to Regular expressions so haven't tried anything myself yet) (我是正则表达式的新手,所以我自己还没有尝试过任何东西)

You can try this regex in PHP: 您可以在PHP中尝试使用此正则表达式:

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

RegEx Demo 正则演示

(?!...) is a non-capturing group . (?!...)是一个非捕获组 Sub-patterns declared within each alternative of this construct will start over from the same index. 在此构造的每个替代方案中声明的子模式将从相同的索引开始。

For simple matches regexp is not neccessary 对于简单的匹配,不需要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";
}

You could use strpos , case-insensitive (Reactive|reactive|REACTIVE) stripos 您可以使用strpos ,不区分大小写(Reactive | reactive | REACTIVEstripos

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM