regex

RonaldS

New member
Jun 17, 2010
288
1
0
I can write a regular expression that will only match strings that are
NOT the word apple:

^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$


But is there a neater way, and how would I do it to match strings that
are NOT the word apple OR banana? Then what would be needed to match
only strings that do not CONTAIN the word "apple" or "banana" or
"cherry"?

I'd love it if the following worked:

[^(apple)(banana)(cherry)]*$

But it appears the parantheses are ignored, as

[^(apple)(banana)(cherry)]*$

simply matches any string that consists entire of the characters
a,b,c,e,h,l,n,r,p & y.

how do i match everything except the word apple in the following sentence

this apple ate an AppLe, APPLE, apple, Apple, applE
(everything except 'apple' should be matched))
 


Yeah, the [^] syntax is a negated character class, not a negated word class. Why not just:
Code:
preg_replace('/\bapple\b/i',' ',$string);
Replaces apple (in a case insensitive way) with a space.

What, at a higher level, are you trying to do? Maybe a regex isn't the best way to do it.