How To Negate a Character Class

Using the ^ ('carrot') to negate a character class:

Metacharacters [] denote a "character class". Within a 'character class' you can have what is called a 'range', which I have been using in the examples above. Lets take a look:
^[c-f]$
This means "the beginning of the line followed by a lowercase letter between c and f (c,d,e, or f), followed by the end of the line."

The carrot character, ^, can also be used inside a character class to "negate" it. negating a character class basically means "not this". Lets take a look..
Eg: ^(.*)[^a-z5-9]*$
This basically means "the beginning of the line followed by zero or more of any character followed by zero or more of any digit or character that is NOT lowercase or 5-9.".

3 comments:

Anonymous

February 17, 2009 at 8:35 PM

It's not the "carrot" character, it's the "caret" character.

biovamps

April 28, 2010 at 11:31 PM

hey sorry for that
will make necessory changes soon

Mike Bosch

February 9, 2011 at 4:02 AM

Good Post. Thanks for sharing.