matching everything but a word using ^ and preg_replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Koñczyk

    matching everything but a word using ^ and preg_replace

    I can't fing a way, to match any characters except the phrase 'foo'.
    Using [^abc]+ matches any character except a, b or c, but I need to not
    match 'foo', because f is ok, o is ok, but foo - 3 letters at once not

    Any clue?

    Thanks

    --
    Daniel Koñczyk
    Daniel Kończyk to niezależny programista specjalizujący się w tworzeniu aplikacji internetowych dostosowanych do potrzeb klienta, bazach danych, testowaniu i optymalizacji.

  • CC Zona

    #2
    Re: matching everything but a word using ^ and preg_replace

    In article <slrnbm6lr2.b6. drmartens@gnu.u niv.gda.pl>,
    Daniel KoŸczyk <drmartens@voru ta.eu.0rg> wrote:
    [color=blue]
    > I can't fing a way, to match any characters except the phrase 'foo'.
    > Using [^abc]+ matches any character except a, b or c, but I need to not
    > match 'foo', because f is ok, o is ok, but foo - 3 letters at once not
    >
    > Any clue?[/color]

    If you just want to match "some pattern when not followed by 'foo'" or
    "some pattern when not preceded by 'foo'", then you're looking for
    lookahead or lookbehind syntax. See the section on "Assertions " in <http://us4.php.net/manual/en/pcre.pattern.sy ntax.php>. For instance, here's two of their examples:

     foo(?!bar)

         matches any occurrence of "foo"  that  is  not  followed  by
         "bar"


     (?<!foo)bar

         does find an occurrence of "bar" that  is  not  preceded  by
         "foo"

    --
    CC

    Comment

    Working...