470,815 Members | 1,103 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

regexp: match negation

Hi,

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?
May 10 '06 #1
1 1777
Rik
Jerry Fleming wrote:
Hi,

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?

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
impossible.

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]);

Grtz,
--
Rik Wasmus
May 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Phil Powell | last post: by
5 posts views Thread by Lukas Holcik | last post: by
reply views Thread by Chris Croughton | last post: by
8 posts views Thread by Dmitry Korolyov | last post: by
7 posts views Thread by Csaba Gabor | last post: by
11 posts views Thread by HopfZ | last post: by
6 posts views Thread by runsun pan | last post: by
27 posts views Thread by SQL Learner | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.