Regular Expression

Predefined character classes

Several character classes are abbreviated by special backslashed letters:

Special
character
Matches aSame as
\ddigit[0-9]
\Dnon-digit[^0-9]
\walphanumeric (word) character[a-zA-Z0-9_]
\Wnon-word character[^a-zA-Z0-9_]
\swhitespace character (space, tab, newline...)[ \t\n\r\f]
\Snon-whitespace character[^ \t\n\r\f]

Other special backslashed letters are:

Special
character
Meaning
\aalarm (beep)
\nnewline
\rcarriage return
\ttab
\fformfeed
\eescape

Example

The following program asks the user to enter date and time in the following format: 08-OCT-1997 16:30, and then checks whether they were entered correctly.
#!/usr/local/bin/perl

print "Please enter date and time, as in \"08-OCT-1997  16:30\"\n";
my $entry = <STDIN>;
chop ($entry);

if ($entry =~ /\d\d-\w\w\w-\d\d\d\d  \d\d:\d\d/) {
   print "good!\n";
} else {
   print "wrong format!\n";
}

# Note: an even better conditional would be:
# if ($entry =~ /[0-3]\d-\w\w\w-\d\d\d\d  [0-2]\d:[0-5]\d/) {   }

Table of Contents.
Next.