在小写或大写n正则表达式中匹配单词组

时间:2014-05-08 12:55:05

标签: php regex preg-match

我需要一个正则表达式来匹配一组大小写的单词 例如,我有一系列的单词:

  

订单,项目,朋友,同学

我想要一个像OrdeRsordersstuDentsFrIendsstudents这样的词来匹配正则表达式。

我真的很赞赏你的帮助。感谢

1 个答案:

答案 0 :(得分:0)

为此,您需要使用不区分大小写的正则表达式i标记。

<?php
$string = "orders,items,friends,students OrdeRs or orders OR stuDents or FrIends or students";

preg_match_all('/(orders|items|friends|students)/i', $string, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[1]); $i++) {
    echo $result[1][$i]."\n";
}
/*
orders
items
friends
students
OrdeRs
orders
stuDents
FrIends
students
*/
?>

DEMO

正则表达式说明:

(orders|items|friends|students)

Options: Case insensitive; 

Match the regex below and capture its match into backreference number 1 «(orders|items|friends|students)»
   Match this alternative (attempting the next alternative only if this one fails) «orders»
      Match the character string “orders” literally (case insensitive) «orders»
   Or match this alternative (attempting the next alternative only if this one fails) «items»
      Match the character string “items” literally (case insensitive) «items»
   Or match this alternative (attempting the next alternative only if this one fails) «friends»
      Match the character string “friends” literally (case insensitive) «friends»
   Or match this alternative (the entire group fails if this one fails to match) «students»
      Match the character string “students” literally (case insensitive) «students»