php preg_match - multivalue pattern

319 views Asked by At

iam using preg_match as follows:

case 1: pattern has two words. with the preg-match solution - it return two results.

case 2: pattern has three words. with the preg-match solution - it return two results.

in my opinion case 2 will return only one result. but i havent any approach.

i tryed with negation - so is the pattern

$pattern="^(^Jim|^goes|^with)"; ) 

or

$pattern="(Jim|goes|with){1}" 

goes wrong or

$pattern="(Jim){1}(with){1}"

goes wrong

for explain:

$pattern1="(Jim|goes|with)";
$_search[0]="Jim Carrey goes crazy";
$_search[1]="Jim Carrey goes crazy with santa clause";

preg_match("/$pattern1/is",$_search[0] )
preg_match("/$pattern1/is",$_search[1] )

is it possible to get an and as pattern for one result in my example ?

THANKS - i hope it

Edit: Input(i) - Output(o) Examples(e)

e1 i: (Jim){1}(goes){1}(with){1}
e1 o: no result

e2 i: (Jim|goes|with)
e2 o: two matches  "Jim Carrey goes crazy" and "Jim Carrey goes crazy with santa clause"


e3 i: ^(^Jim|^goes|^with)
e3 o: two matches  "Jim Carrey goes crazy" and "Jim Carrey goes crazy with santa clause"

which imput solution comes with one result ?

which solution with input: "Jim goes with" generate one result for example:"Jim Carrey goes crazy with santa clause" it means an and condition in regex - but is it possible ?

SOLUTION:

 $patternsearch=chop("Jim goes with  ");

 if(preg_match('/ /',$patternsearch)){
 $_array_pattern = explode( ' ', $patternsearch );
 $text = preg_replace('/ /','|',$patternsearch); 
 $pattern1=''.'('.$text.')';
 }else
 {
 $pattern1 = $patternsearch;
 }

 echo "my search is as follow: $patternsearch"."</br>";
 echo "my pattern is as follow: $pattern1"."</br></br>";


foreach($_search as $search){

$andcounter= preg_match_all("/$pattern1/isx", $search,$matscha);

echo "preg_match count:  $andcounter =";
echo "search count :  ".count($_array_pattern)."</br></br>";

if(count($_array_pattern) === $andcounter ){

$item[]=$search;

}



} 
echo "<pre>";
var_dump($item);
echo "</pre>" ;

OUTPUT:

my search is as follow: Jim goes with

my pattern is as follow: (Jim|goes|with)

preg_match count: 2 =search count : 3

preg_match count: 3 =search count : 3

array(1) {
   [0] =>
   string(39) "Jim Carrey goes crazy with santa clause"
}

and with:

     $patternsearch="Jim goes  ";

my search is as follow: Jim goes

my pattern is as follow: (Jim|goes)

preg_match count: 2 =search count : 2

preg_match count: 2 =search count : 2

array(2) {
   [0] =>
   string(21) "Jim Carrey goes crazy"
   [1] =>
   string(39) "Jim Carrey goes crazy with santa clause"
}
3

There are 3 answers

1
lu cip On BEST ANSWER

Hei,

What about using preg_match_all (http://php.net/manual/en/function.preg-match-all.php) with $pattern="(Jim|goes|with)";

preg_match_all("/$pattern/is", $_search[0] ) // return 2
preg_match_all("/$pattern/is", $_search[1] ) // return 3
0
davide On

I think there is a bit of confusion here on how to use regex. If you use /Jim|goes|with/, it means you want to match any string that contains any of Jim, goes, with. If you also surround your regex with parenthesis, i.e. /(Jim|goes|with)/ you also capture the match. Now, since both the sentences you want to match have Jim and goes, you always have a positive match.

Now, if you want to have a positive match for a sentence that contains ALL the words Jim, 'goes' and 'with', you need something like:

/\bJim\b.*?\bgoes\b.*?\bwith/

where '\b' means word boundary and avoid you to have false matches with something like "Jimmy", "mangoes" or "without", . means more or less "any character" (there are subtleties related to newlines) and *? is a reluctant quantifier which is well explained here Greedy vs. Reluctant vs. Possessive Quantifiers.

I strongly suggest you to take a look at least here (http://en.wikipedia.org/wiki/Regular_expression) for some very basic info.

0
Barmar On

I'm not totally sure I understand the question, but maybe this is what you want:

$pattern = '(Jim)?.*(goes)?.*(crazy)?';
if (preg_match("/$pattern/is", $_search[0], $match)) {
    $num_matches = count(array_filter($match)) - 1;
}

array_filter will remove the empty matches, which occur when the optional string isn't found. We then subtract 1 from the count, because $match[0] is the match of the entire regexp, which you don't care about.