RE: looking a regular expresion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>> -----Original Message-----

>> From: Manuel Pérez López [mailto:mapelo@xxxxxxxxx]

>> Sent: Tuesday, May 27, 2008 2:06 PM

>> To: php-general@xxxxxxxxxxxxx

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux