10. Predefined Classes
Join the Discussion
A couple of tutorials back we looked at character classes that can be defined using . While any combination of alternative characters can be defined using character classes as described in that tutorial there are a few classes that are so common that an alternative shorter notation was developed that can be used in place of those common classes.
The most obvious character class that deserves (and has) a shorter alternative is the class that tests for numeric. Instead of specifying [0-9] we can instead simply specify \d which means the same thing. The backslash (slosh) in front of the d escapes its regular meaning as a lowercase 'd' character and gives it the alternative meaning of any digit (ie. 0 through 9) instead.
There are a number of additional predefined classes that you will find useful.
All of these predefined classes use the same format of escaping a letter of the alphabet to give that letter a special meaning. There is one more predefined class that has a special meaning without being escaped (and which therefore needs to be escaped to give it back its normal character meaning. That character is the period (.) and will match any character except for a line feed or carriage return.