简体   繁体   中英

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)

It should return FALSE if these keywords are not found.

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:

(?|(\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

<?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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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