使用正则表达式和php提取所有出现的模式

时间:2014-11-20 07:20:52

标签: php regex

我想提取'ob'','之间出现的所有字词,即 来自字符串

  

' QS=TCE,Ob=Web technology,OB=Product SPe,OB=Dev profile,OB=Computer Management,oB=Hardware Services,cd=sti,CD=com,cd=ws'

我想要以下结果:

  

Web technology,Product SPe,Dev profile,Computer Management,Hardware Services

我试过的是:

preg_match_all("!\OB=(\w+)\,!", ' QS=TCE,Ob=Web technology,OB=Product SPe,OB=Dev profile,OB=Computer Management,oB=Hardware Services,cd=sti,CD=com,cd=ws', $matches);
print_r($matches);

但它没有给出任何结果。这有什么问题?

1 个答案:

答案 0 :(得分:2)

你需要考虑你的单词之间的空白,我会在这里使用否定。您还需要删除“O”和逗号之前的转义序列,并打开i(不区分大小写)模式修饰符。

preg_match_all('!OB=([^,]+),!i', $str, $matches);
print_r($matches[1]);

Working Demo

相关问题