Reply to Re: regexp: match negation

Your name:

Reply:


Posted by Rik on 05/10/06 06:35

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация