Summary: in this tutorial, you will learn how to use character classes to build regular expressions that represent the whole classes of characters.
In the previous Perl regular expresssion tutorial, we’ve built regular expressions with literal strings, for example
/world/. However, regular expression engine allows you to build regular expressions that represent not just only a single character sequence but also a whole class of them, for example, digits, whitespace and words.
You use square brackets
 to denote the character classes in regular expressions. Inside the square brackets, you put a set of characters that possibly matches.
Take a look at the following examples:
my @words = ('dog','frog','chicken');
print("$_\n") if(/[dfr]og/); # matches dog, frog
The regular expression
/[dfr]og/ matches dog, frog, dog, etc.
The regular expression below:
/elem/; # matches 'elem0', 'elem1'... or 'elem5'
If you want to match from
elem1000, you can use range operator (-) within the character classes, for examples:
/[a-z]1/; matches a1,b1,... to z1
To make the regular expressions more readable, Perl provides useful predefined abbreviations for common character classes as shown below:
\dmatches a digit, from 0 to 9 [0-9]
\smatches a whitespace character, that is a space, tab, newline, carriage return, formfeed. [\t\n\r\f]
\wmatches a “word” character (alphanumeric or _) [0-9a-zA-Z_].
And the following are negative predefined character classes:
\Dmatches any non-digits [^0-9]
\Smatches any non-whitespace character [^\t\n\r\f]
\Wmatches any non-word character [^0-9a-zA-Z_]
Since version 5.14, Perl uses //a modifier to restrict the matches of
\w to characters in ASCII range.
The following example demonstrates how to use predefined character classes:
my $time = localtime();
print $time, "\n";
print "Match found\n" if($time =~ /\d\d:\d\d:\d\d/);
The output of the program is:
Fri Dec 28 11:11:13 2012
We used the
localtime() function to get the formatted date and time as an ASCII string. The regular expression
/\d\d:\d\d:\d\d/ matched the time in
In this tutorial, you’ve learned how to use character classes in regular expressions to match the whole classes of characters.