RE: [PHP] looking a regular expresion

From: Date: Wed, 28 May 2008 19:57:41 +0000
Subject: RE: [PHP] looking a regular expresion
Groups: php.general 
Request: Send a blank email to php-general+get-274781@lists.php.net to get a copy of this message
>> -----Original Message----- >> From: Manuel Pérez López [mailto:mapelo@gmail.com] >> Sent: Tuesday, May 27, 2008 2:06 PM >> To: php-general@lists.php.net >> Subject: [PHP] looking a regular expresion >> >> Hello: >> >> I need to include a pair of negations with two complete word into a >> regular expresion for preg_replace. How to do this? >> I want to replace "I want to be a SUN and a SIR" with "FRIKI FRIKI >> FRIKI FRIKI FRIKI SUN FRIKI FRIKI SIR" >> >> ie. the words are: SUN and SIR. And the replacement word is: FRIKI >> >> $st = preg_replace ("\b([^S][^U][^N])|([^S][^I][^R]\b)", "FRIKI",$st); >> >> This does not match >> >> Anyone hep me? >> >> Thanks > >Man... this is really irritating the heck out of me. I have recently been learning regular >expressions, and >thought I was "fresh" enough to fix this with new eyes. However, >I've been running into all kinds of trouble, >and I now impatiently anticipate an >expert's answer, much the same as you. :) > >It's interesting to me that `$st = preg_replace("(SUN)", >"FRIKI", $st); replaces the word "SUN" with the >>word "FRIKI", but $st = >preg_replace("(^SUN)", "FRIKI", $st);` does the same thing (when >it should be >replacing everything BUT the word "SUN"). > >Annoying behavior. Is this particular to PHP's regular expression engine? I should try this >in VB.NET and see >what the results are, as that's what I'm used to playing with (for >the time being). Okay, I think I figured it out! $st = "SUN ASDF SIR BLAH SIR SIR SUN"; $st = preg_replace ("/(?!\b(SUN|SIR)\b)\b\w+\b/", "FRIKI", $st); echo $st; It involves a "lookaround," so I'm not sure how efficient it is-but it works! Man, that's a load off my mind. It's not as simple as it would have seemed at first... but now that I have a look at the finished regex string, it makes sense. "If there's not a word boundary followed by either SIR or SUN followed by another word boundary, start over. Now, if there's a word boundary followed by 1 or more word characters followed by another word boundary, replace that with FRIKI." Hope this helps!! Todd Boyd Web Programmer

« previous php.general (#274781) next »