Perl Escape Characters
This page contains a list of Perl Escape characters. The ones that are the most immediately useful are the position escape characters and the
character-range escape characters. You will also find a list of non-alphanumeric escape characters at the end of this page.
Positions
Character | Value |
\b | on a word boundary (i.e. either at the start or end of a word depending on where the escape character appears in the regular expression) |
\B | inside a word boundary (i.e. only if a pattern is contained in a word) |
\A | beginning of string |
\Z | end of string |
\G | where previous m//g left off |
Example
Now let's apply the word boundary escape character to our yes/no if
structure:
if($input=~/^yes\b/i
{ print "Let us play!\n" }
else
{ print "Okay. Thanks anyway.\n" }
The \b
tells the script to look for the end of a word or a word boundary after yes
. This will only work for yes
. It allows us to ignore any words longer than yes
.
In other words, y
and yesterday
would fail.
Regular Expressions
Character classes
Character | Value |
\w | any alphanumeric (a..z, A..Z, 0..9, _) |
\W | any nonalphanumeric |
\s | any whitespace (\n \r \f \t \x20) |
\S | any non-whitespace |
\d | any digit |
\D | any nondigit |
Non-alphanumerics
Character | Value |
\nnn | an ASCII value in octal |
\xnn | an ASCII value in hexadecimal |
\cx | ASCII control-x |
\n | newline (\x0a ) |
\r | carriage-return (\x0d ) |
\f | form-feed (\x0c ) |
\t | tab |
\a | alarm (beep \x07 ) |
\e | escape (\x1b ) |
\\ | backslash (\x5c ) |