簡體   English   中英

使用正則表達式和php提取所有出現的模式

[英]Extract all occurrences of a pattern using regular expression and php

我想從字符串中提取出現在'ob'','之間'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);

但這沒有任何結果。 這有什么問題?

您需要考慮單詞之間的空格,在這里我將使用否定。 另外,您需要刪除“ O”和逗號前面的轉義序列,並打開i (不區分大小寫)模式修飾符。

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

工作演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM