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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.