Skip to Content
Regular Expressions Cookbook, 2nd Edition
book

Regular Expressions Cookbook, 2nd Edition

by Jan Goyvaerts, Steven Levithan
August 2012
Intermediate to advanced
609 pages
19h 16m
English
O'Reilly Media, Inc.
Content preview from Regular Expressions Cookbook, 2nd Edition

2.2. Match Nonprintable Characters

Problem

Match a string of the following ASCII control characters: bell, escape, form feed, line feed, carriage return, horizontal tab, vertical tab. These characters have the hexadecimal ASCII codes 07, 1B, 0C, 0A, 0D, 09, 0B.

This demonstrates the use of escape sequences and how to reference characters by their hexadecimal codes.

Solution

\a\e\f\n\r\t\v
Regex options: None
Regex flavors: .NET, Java, PCRE, Python, Ruby
\x07\x1B\f\n\r\t\v
Regex options: None
Regex flavors: .NET, Java, JavaScript, Python, Ruby
\a\e\f\n\r\t\x0B
Regex options: None
Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby

Discussion

Seven of the most commonly used ASCII control characters have dedicated escape sequences. These all consist of a backslash followed by a letter. This is the same syntax that is used by string literals in many programming languages. Table 2-1 shows the common nonprinting characters and how they are represented.

Table 2-1. Nonprinting characters

Representation

Meaning

Hexadecimal representation

Regex flavors

\a

bell

0x07

.NET, Java, PCRE, Perl, Python, Ruby

\e

escape

0x1B

.NET, Java, PCRE, Perl, Ruby

\f

form feed

0x0C

.NET, Java, JavaScript, PCRE, Perl, Python, Ruby

\n

line feed (newline)

0x0A

.NET, Java, JavaScript, PCRE, Perl, Python, Ruby

\r

carriage return

0x0D

.NET, Java, JavaScript, PCRE, Perl, Python, Ruby

\t

horizontal tab

0x09

.NET, Java, JavaScript, PCRE, Perl, Python, Ruby

\v

vertical tab

0x0B

.NET, Java, JavaScript, Python, Ruby

In Perl 5.10 and later, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Regular Expressions Cookbook

Regular Expressions Cookbook

Jan Goyvaerts, Steven Levithan

Publisher Resources

ISBN: 9781449327453Supplemental ContentErrata Page