Re: hot to use perg_match with multi strign patern

From: Date: Wed, 07 May 2003 19:55:26 +0000
Subject: Re: hot to use perg_match with multi strign patern
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-146640@lists.php.net to get a copy of this message
Dear Stefan, Thank you for your advise I try strpos as this; $pos = strpos($mesage, "/(?:yes|no)/i"); but it returns nothing, do I need different patern design for strpos? <SLanger@spirit21.de> wrote in message news:OF74814AF1.117DE5CA-ONC1256D1F.00316CA7-41256D1F.003744CC@spirit21.de.. . > Hello > > Your problem is the pattern itself. > > >if (preg_match("/(yes)?(no)/i", $mesage, $matches)) { > > print "refusee - $matches[0]"; > >} else { > > print "negative"; > >}; > > Your pattern tells the regex to find any 'no' in a text. Since the yes is > optional the regex will never check for it. In other words the ()? part is > skipped and the no part is tested since the pattern can only match if a no > is in the text. The regex will see this and simply ignore the (yes)?. > > What you probably want is to use alternatives making your pattern look > like /(?:yes|no)/i. This way either yes or no is found. Beaware that if > you do want to do this it will probably faster to use a string function to > find literal occurences. > > Regards > Stefan Langer

« previous php.general (#146640) next »