Shery
Shery

Reputation: 1882

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)

Upvotes: 1

Views: 98

Answers (3)

anubhava
anubhava

Reputation: 785156

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.

Upvotes: 1

Isaac
Isaac

Reputation: 465

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

Upvotes: 0

cske
cske

Reputation: 2243

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

Upvotes: 0

Related Questions