Inverse Character Classes

In the last example we introduced the concept of character classes to match a given set of characters in a search string. In addition to creating a character class you can also create the inverse of a character class. For example, the character class:

 

[A-Z]

 

will match any character between A and Z in the ASCII character set (i.e. A, B, C, D ... but not a, b, c, d). However, if we wanted to match anything but A-Z we could complement or invert the character class as follows:

 

[^A-Z]

 

The ^ operator inverts the character class. Note that it must be the first character inside the square brackets. If it is not the first character then it has no special meaning, it is just the ^ character. This new expression will match a, b, c, d, 9, #, $ or any other character expect ASCII characters in the range of A-Z.

 

Now we'll introduce the plus regex character (+) to make our character class match more than one character in a row.

 

 

Back: Character Classes

Forward: The 'plus' character