Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to exclude 1 word out of a regex code

Tags:

regex

php

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.

like image 759
Mech Avatar asked Apr 07 '10 18:04

Mech


People also ask

How do you exclude words in regex?

To represent this, we use a similar expression that excludes specific characters using the square brackets and the ^ (hat). For example, the pattern [^abc] will match any single character except for the letters a, b, or c.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do you regex only words?

To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.


1 Answers

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

like image 107
Hun1Ahpu Avatar answered Oct 18 '22 17:10

Hun1Ahpu