XML technologies / Regular expressions / Features and dialects / Common metacharacters / Character representations

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

This group of metacharacters provides convenient representations for characters otherwise hard to express.

 

Shorthand metacharacters

Many programs provide shorthands for control characters that are hard to type and may depend on the operating system:

\a

Alert (beep, producing a sound when output). Usually ASCII <BEL>, octal 007.

\b

Backspace. ASCII <BS>, octal 010. (In many dialects, ˹\b˼ is shorthand only inside classes; elsewhere it denotes a word boundary, see , ....">Word boundaries: \b, \B, \<, \>, ....)

\e

Escape. Usually ASCII <ESC>, octal 033.

\f

Form feed (form feed). Usually ASCII <FF>, octal 014.

\n

Newline (newline). On most systems, notably Unix and DOS/Windows, the line-feed character, ASCII <LF>, octal 012. On MacOS, ASCII <CR>, octal 015. In Java and .NET languages, always ASCII <LF>, regardless of the operating system.

\r

Carriage return (carriage return). Usually ASCII <CR>. On MacOS, ASCII <LF>. In Java and .NET languages, always ASCII <CR>, regardless of the operating system.

\t

Tab (tab). The ordinary horizontal tab, usually ASCII <HT>, octal 011.

\v

Vertical tab. Usually ASCII <VT>, octal 013.

 

The table lists common tools and languages and their supported shorthands. As noted earlier, some languages use the same escapes in strings. Strings as regular expressions also explains the dangers of confusing string and regex metacharacters.

Are these characters platform-dependent?

As listed above, the meanings of \n and \r depend on the operating system in most tools and languages (note: a C or C++ program translating regex escapes to C string escapes gets compiler-dependent characters. Compilers for a given system generally agree, so “OS-dependent” is reasonable. Only \n and \r appear to vary between systems, increasingly rarely; the others are effectively standard). Pay attention to this. If you simply need a newline, use \n. For a specific value, such as in a standard protocol like HTTP, use \012 or whatever the protocol requires (\012 is an octal escape for ASCII LF). For a DOS line ending, use ˹\015?\012˼ (these match the ending characters; to match the position at the beginning or end, use anchors; see Anchors and other zero-width assertions).

 

Table: programs and supported shorthand metacharacters.

Program

Shorthand metacharacters

\b
Word boundary

\b
Backspace

\a
Alert

\e
ASCII-ESC

\f
Form feed

\n
Newline

\r
Carr. ret.

\t
Tab

\v
Vertical tab

Python

✓

✓c

✓

✓

✓

✓

✓

✓

Tcl

yes, \y

✓

✓

✓

✓

✓

✓

✓

✓

Perl

✓

✓c

✓

✓

✓

✓

✓

✓

Java

✓x

✓x

✓

✓

✓sr

✓sr

✓sr

✓sr

✓

GNU awk

✓

✓

✓

✓

✓

✓

✓

GNU sed

✓

✓

GNU Emacs

✓x

✓s

✓s

✓s

✓s

✓s

✓s

✓s

✓s

.NET

✓

✓c

✓

✓

✓

✓

✓

✓

✓

PHP (preg package)

✓

✓c

✓

✓

✓

✓

✓

✓

MySQL

GNU grep/egrep

✓

flex

✓

✓

✓

✓

✓

✓

✓

Ruby

✓

✓c

✓

✓

✓

✓

✓

✓

✓

  

✓

supported

✓c

supported only in character classes

✓sr

supported, also as a string metacharacter

✓x

supported, with a different meaning in strings

✓x

unsupported, with a different meaning in strings

✓s

unsupported, but supported as a string metacharacter

The table assumes the most regex-friendly string type is used (see Strings as regular expressions)

Octal escapes: \Zahl

Many implementations allow two- or three-digit octal notation (base 8) for ASCII bytes. For example, ˹\015\012˼ matches ASCII CR/LF, convenient for hard-to-type characters. Perl supports ˹\e˼ for ASCII ESC, unlike awk. However, awk supports octal escapes, allowing the ESC code directly: ˹\033˼.

The following table lists supported octal escapes.

In some implementations, ˹\0˼ specially matches NUL. Others allow one-digit octal escapes, usually without backreferences such as ˹\1˼. Ambiguity usually favors backreferences. Some languages allow four octal digits, generally requiring an initial zero (for example, java.util.regex).

What about out-of-range values like \565? Eight-bit octal values range from \000 to \377. Roughly half the programs automatically use more than one byte internally, potentially matching a Unicode character. The other half truncates high bits, producing \165, corresponding to ASCII ›u‹. Restrict octal escapes to \377.

 

Hexadecimal and Unicode escapes: \xZahl, \x{Zahl}, \uZahl, \UZahl, ...

Similarly, some programs allow hexadecimal escapes (base 16), prefixing the digits with \x, \u, or sometimes \U. With \x supported, CR/LF becomes ˹\x0D\x0A˼. The table also lists hexadecimal escapes.

Besides the right escape, the allowed digit count and whether braces are allowed or required matter. The table specifies this too.

Table: programs and supported octal and hexadecimal escapes.

Backreferences

Octal escapes

Hexadecimal escapes

Python

✓

\0, \07, \377

\xFF

Tcl

✓

\0, \77, \777

\x... \uFFFF, \UFFFFFFFF

Perl

✓

\0, \77, \777

\xF, \xFF, \x{...}

Java

✓

\07, \077, \0377

\xFF, \uFFFF

GNU awk

\7, \77, \377

\x...

GNU sed

✓

GNU Emacs

✓

.NET

✓

\0, \77, \377

\xFF, \uFFFF

PHP (preg functions)

✓

\0, \77, \377

\xF, \xFF, \x{...}

MySQL

GNU egrep

✓

GNU grep

✓

flex

\7, \77, \377

\xF, \xFF

Ruby

✓

\7, \77, \377

\xF, \xFF

\0

˹\0˼ matches NUL; otherwise no one-digit octal escapes

\7, \77

one- and two-digit octal escapes supported

\07

two-digit octal escapes require a leading zero

\077

three-digit octal escapes require a leading zero

\377

three-digit octal escapes up to \377 supported

\0377

four-digit octal escapes up to \0377 supported

\777

three-digit octal escapes up to \777 supported

\x...

\x allowed with any number of digits

\x{...}

\x{...} allowed with any number of digits

\xF, \xFF

one- and two-digit hexadecimal escapes with \x supported

\uFFFF

four-digit hexadecimal escapes with \u supported

\UFFFF

four-digit hexadecimal escapes with \U supported

\UFFFFFFFF

eight-digit hexadecimal escapes with \U supported

For program versions, see Program versions in this book.

 

Control characters: \cZeichen

Many dialects use the sequence \cZeichen for control characters below ASCII 32 (some allow more). For example, ˹\cH˼ matches Ctrl-H, backspace; ˹\cJ˼ matches ASCII line feed, also often matched by ˹\n˼, but sometimes by ˹\r˼, depending on the system; see Shorthand metacharacters.

Details vary. Uppercase ASCII letters are safest. Most implementations also accept lowercase, but Sun's Java package does not. Nonletters depend on the dialect; I recommend uppercase only with \c.

GNU Emacs uses another sequence for this purpose: ˹?\^Zeichen˼ (so ˹?\^H˼ matches Ctrl-H, ASCII backspace).

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top