簡體   English   中英

兩個或兩個以上單詞的正則表達式php

[英]Regular expressions for two or more words php

我有以下職位:

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

我想搭配:

Reactive Coach (與Reactive關鍵字或Coach關鍵字在字符串中的位置無關)也希望與Engineer關鍵字匹配(同樣可以在字符串中的任何位置出現)

如果找不到這些關鍵字,則應返回FALSE

對於上述情況,什么是合適的正則表達式? (我是正則表達式的新手,所以我自己還沒有嘗試過任何東西)

您可以在PHP中嘗試使用此正則表達式:

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

正則演示

(?!...)是一個非捕獲組 在此構造的每個替代方案中聲明的子模式將從相同的索引開始。

對於簡單的匹配,不需要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";
}

您可以使用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