Minase Posted January 18, 2010 Share Posted January 18, 2010 Hello i was wondering how can i make a regex that can search with syntax OR and AND in same time something like text = cat,dog,mouse text2 = cat,dog of course i dont want any * to match every character,instead i would like to check if a specific value is there and if it is to find it something like a combination between | and & thank you Quote Link to comment https://forums.phpfreaks.com/topic/188943-simple-regex-question/ Share on other sites More sharing options...
Minase Posted January 18, 2010 Author Share Posted January 18, 2010 please i really need help with this one ... :| here is what im trying to achieve text = cat,dog,mouse text2 = cat,dog Regex = cat|dog|mouse match(regex,text) = cat,dog,mouse match(regex,text2) = cat,dog i want to match every regex inside it's more like optional items thank you Quote Link to comment https://forums.phpfreaks.com/topic/188943-simple-regex-question/#findComment-997727 Share on other sites More sharing options...
nrg_alpha Posted January 19, 2010 Share Posted January 19, 2010 So do you mean something like this? $str = 'I like my cat better than my dog, but decided to eat my mouse instead.'; preg_match_all('#(?:cat|dog|mouse)#i', $str, $matches); // using preg_match_all will match any / multiple instances of what the pattern is looking for echo '<pre>'.print_r($matches, true); Quote Link to comment https://forums.phpfreaks.com/topic/188943-simple-regex-question/#findComment-997730 Share on other sites More sharing options...
Minase Posted January 19, 2010 Author Share Posted January 19, 2010 thank you for this but the problem is that i want to check for optional group and if is found then store all values found till end of regex in a array let me give a better example String <br>Forta +95<br>Aparare +20<br>Dexteritate +20<br> <br>Forta +70<br>Dexteritate +80<br> it should stop searching at line end (this is not a problem i can make regex to do that) here is what i try to achieve Regex <br>(Forta (.*?)<br>) | (Aparare (.*?)<br>) | (Dexteritate (.*?)<br>) so the first match will be = Forta 95,Aparare 20,Dexteritate 20 The second = Forta 70,Dexteritate 80 Hope you understand what i mean thank you Quote Link to comment https://forums.phpfreaks.com/topic/188943-simple-regex-question/#findComment-997741 Share on other sites More sharing options...
Minase Posted January 19, 2010 Author Share Posted January 19, 2010 hmm it does seem that the OR ( |) operator solved my problem (SYNTAX|) it's logical if you think about it Thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/188943-simple-regex-question/#findComment-997755 Share on other sites More sharing options...
nrg_alpha Posted January 19, 2010 Share Posted January 19, 2010 I suppose one way could something like: $html = <<<EOF <br>Forta +95<br>Aparare +20<br>Dexteritate +20<br> <br>Forta +70<br>Dexteritate +80<br> EOF; preg_match_all('#(?:<br>(?:Forta|Aparare|Dexteritate).*?(?=<br>))+#i', $html, $matches); foreach($matches[0] as $val){ echo $val . "<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/188943-simple-regex-question/#findComment-997757 Share on other sites More sharing options...
Minase Posted January 19, 2010 Author Share Posted January 19, 2010 that work very well too thank you for your time Quote Link to comment https://forums.phpfreaks.com/topic/188943-simple-regex-question/#findComment-997769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.