regexp: match negation

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I want to strip part of a string that *doesn't* match a patter. But  
preg_replace does the opposite. For example:

$str = 'aa bb';
echo preg_replace('/bb|cc/', '', $str);

where aa is unpredictable, so my pattern can only be bb|cc. The problem  
is, how can I replace aa by negating a pattern?

Re: regexp: match negation

Jerry Fleming wrote:
Quoted text here. Click to load it

Very difficult with preg_replace. If you know _exactly_ what is allowed, why
not preg_match() the string and continue working with the output from that
function? Negating characters, getting parts NOT followed or predeeded by a
specific string can be done, but negating whole words is as far as I know

For instance:

Allowed are 'cat' & 'dog' & whitespacecharacters.
$tring = "dog   dogcat\ncowcat";
preg_match_all('/(cat|dog|\s)/si', $tring, $matches, PREG_PATTERN_ORDER);
$allowedstring = implode($matches[1]);

Rik Wasmus

Site Timeline