Sunday, January 18, 2009

Character Classes

Sometimes it is necessary to not exactly match text or numbers. In a character class you can list all possible characters or a range of characters that do the match. Let’s move away from our sample text a little and try some numeric and alpha numeric matching.

[0-9]*

0815

This will match any number. The characters must be in the range of 0 to 9.


[19]*.[the]*

199 mountains reach the skies.

Use the step over button ->> in The RegEx Coach to see what matches.

You would read a 1 or 9 Zero or N times + dot ( any character ) + t, h or e Zero or N times.

Negated character classes

It is also possible to create character classes by excluding characters or ranges. This is done by putting a ^ after the opening bracket.

[^0-9].*

mountains reach the skies.

This will match the string but will not match
199 mountains reach the skies.