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:
\aAlert (beep, producing a sound when output). Usually ASCII
<BEL>, octal007.\bBackspace. ASCII
<BS>, octal010. (In many dialects, ˹\b˼ is shorthand only inside classes; elsewhere it denotes a word boundary, see , ....">Word boundaries: \b, \B, \<, \>, ....)\eEscape. Usually ASCII
<ESC>, octal033.\fForm feed (form feed). Usually ASCII
<FF>, octal014.\nNewline (newline). On most systems, notably Unix and DOS/Windows, the line-feed character, ASCII
<LF>, octal012. On MacOS, ASCII<CR>, octal015. In Java and .NET languages, always ASCII<LF>, regardless of the operating system.\rCarriage return (carriage return). Usually ASCII
<CR>. On MacOS, ASCII<LF>. In Java and .NET languages, always ASCII<CR>, regardless of the operating system.\tTab (tab). The ordinary horizontal tab, usually ASCII
<HT>, octal011.\vVertical tab. Usually ASCII
<VT>, octal013.
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 | \b | \a | \e | \f | \n | \r | \t | \v | |
✓ | ✓c | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
Tcl | yes, | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
✓ | ✓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 | ✓ |
|
|
Tcl | ✓ |
|
|
Perl | ✓ |
|
|
Java | ✓ |
|
|
GNU awk |
|
| |
GNU sed | ✓ | ||
GNU Emacs | ✓ | ||
.NET | ✓ |
|
|
PHP (preg functions) | ✓ |
|
|
MySQL | |||
GNU egrep | ✓ | ||
GNU grep | ✓ | ||
flex |
|
| |
Ruby | ✓ |
|
|
|
| ||
| one- and two-digit octal escapes supported | ||
| two-digit octal escapes require a leading zero | ||
| three-digit octal escapes require a leading zero | ||
| three-digit octal escapes up to | ||
| four-digit octal escapes up to | ||
| three-digit octal escapes up to | ||
|
| ||
|
| ||
| one- and two-digit hexadecimal escapes with | ||
| four-digit hexadecimal escapes with | ||
| four-digit hexadecimal escapes with | ||
| eight-digit hexadecimal escapes with | ||
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