简体   繁体   中英

Extract all occurrences of a pattern using regular expression and php

I want to extract all the word that occurs between 'ob' and ',' , ie, from the string

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

I want the following results:

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

What I tried is this:

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);

But it does not give any result. What is wrong in this?

You need to account for whitespace between your words, I would use negation here. Also you need to remove the escape sequences preceding the "O" and comma and turn on the i (case-insensitive) mode modifier.

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

Working Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM