Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a program to match all occurences of &, but not any of &

I have been able to generate &[^amp;] for my patern, but this negates matching on &a, &m, &p, &; which I would like to find.... I tried &[^a][^m][^p][^;], but that still blocks &a

How on earth do I do this?

Replies are listed 'Best First'.
Re: using regex to not match a string
by Tanktalus (Canon) on Apr 18, 2005 at 23:52 UTC

    [...] is a character class. You want (?!...) (see perlre). e.g., /&(?!amp;)/

    Update: Darn. Too slow. :-) (Although I got the semicolon ;->)

Re: using regex to not match a string
by tlm (Prior) on Apr 18, 2005 at 23:51 UTC

    Oops. Didn't notice the ; at the end of the pattern you want to avoid.

    What Tanktalus said: /&(?!amp;)/ .

    /&(?!amp\b)/
    The above will match &, &foo, &ersand, but not & followed by a \W character or by the end of the string. If you don't want to match &, irrespective of what follows it, then use
    /&(?!amp)/
    See ?! in perlre.

    the lowliest monk