我的职位名称如下:
Reactive Customer Coach
Customer Reactive Coach
Technical Reactive Customer Coach
Field Engineer
Customer Engineer for FTTC
我想要匹配:
Reactive Coach
(字符串中出现Reactive
关键字或Coach
关键字无关紧要)
还希望匹配Engineer
关键字(同样可以出现在字符串中的任何位置)
如果找不到这些关键字,则应返回FALSE
。
对于上述场景,什么是合适的正则表达式? (我是正则表达式的新手,所以我自己还没有尝试过任何东西)
答案 0 :(得分:1)
你可以在PHP中尝试这个正则表达式:
(?|(\bReactive\b).*?(\bCoach\b(?! *OM\b))|(\bCoach\b).*?(\bReactive\b)|(\bEngineer\b))
(?!...)
是非捕获组。在此构造的每个替代项中声明的子模式将从相同的索引开始。
答案 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)