5

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.

1
  • Your regex does not work if the search text starts with GC... Commented Apr 7, 2010 at 18:34

3 Answers 3

10

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 ?<!

Sign up to request clarification or add additional context in comments.

Comments

0

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]+/

Comments

0

See if this works:

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

More Information is available at lookaround

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.