QuantumRob
QuantumRob

Reputation: 2924

Regex to exclude 1 word out of a regex code

I need a regex expert to help out on this one. Examples I've found on here and the net I cant seem to get right. I'm using PHP and I have the following regex expression

/([^a-zA-Z0-9])GC([A-Z0-9]+)/

This matches items like GCABCD GC123A, etc. What i need to do is EXCLUDE GCSTATS from this. So basically I want it to work just as it has, except, ignore GCSTATS in the regex.

Upvotes: 5

Views: 9212

Answers (3)

Hun1Ahpu
Hun1Ahpu

Reputation: 3355

Try to add this after GC: (?!STATS). It's a negative lookahead construction. So your regex should be

/([^a-zA-Z0-9]*)GC(?!STATS)([A-Z0-9]+)/

p.s. or try ?<!

Upvotes: 10

Audie
Audie

Reputation: 1470

See if this works:

([^a-zA-Z0-9])GC((?!STATS)[A-Z0-9]+)

More Information is available at lookaround

Upvotes: 0

OIS
OIS

Reputation: 10033

Regex assertions is what you need. This also works if the search text starts with GC...

/(?<![A-Za-z0-9])GC(?!STATS)[A-Z0-9]+/

Upvotes: 0

Related Questions