regexp: match negation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry Fleming

    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?
  • Rik

    #2
    Re: regexp: match negation

    Jerry Fleming wrote:[color=blue]
    > 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?[/color]


    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' & whitespacechara cters.
    $tring = "dog dogcat\ncowcat" ;
    preg_match_all( '/(cat|dog|\s)/si', $tring, $matches, PREG_PATTERN_OR DER);
    $allowedstring = implode($matche s[1]);

    Grtz,
    --
    Rik Wasmus


    Comment

    Working...