Jump to content

Simple Regex Question


Minase

Recommended Posts

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 * :P 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

Link to comment
https://forums.phpfreaks.com/topic/188943-simple-regex-question/
Share on other sites

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

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

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

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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.