Regular Expressions   «Prev 

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

\bon 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)
\Binside a word boundary
(i.e. only if a pattern is contained in a word)
\Abeginning of string
\Zend of string
\Gwhere 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

\wany alphanumeric (a..z, A..Z, 0..9, _)
\Wany nonalphanumeric
\sany whitespace (\n \r \f \t \x20)
\Sany non-whitespace
\dany digit
\Dany nondigit

Non-alphanumerics


Character

Value

\nnnan ASCII value in octal
\xnnan ASCII value in hexadecimal
\cxASCII control-x
\nnewline (\x0a)
\rcarriage-return (\x0d)
\fform-feed (\x0c)
\ttab
\aalarm (beep \x07)
\eescape (\x1b)
\\backslash (\x5c)