POSIX-Style Regular Expressions
Now that you understand the basics
of regular expressions, we can explore the details. POSIX-style
regular expressions use the
Unix locale
system. The locale system provides functions for sorting and
identifying characters that let you intelligently work with text from
languages other than English. In particular, what constitutes a
“letter” varies from language to
language (think of à and
ç), and there are character classes in
POSIX regular expressions that take this into account.
However, POSIX regular expressions are designed for use with only
textual data. If your data has a NUL-byte (\x00)
in it, the regular expression functions will interpret it as the end
of the string, and matching will not take place beyond that point. To
do matches against arbitrary binary data, you’ll
need to use Perl-compatible regular expressions, which are discussed
later in this chapter. Also, as we already mentioned, the Perl-style
regular expression functions are often faster than the equivalent
POSIX-style ones.
Character Classes
As shown in Table 4-7, POSIX defines a number of named sets of characters that you can use in character classes. The expansions given in Table 4-7 are for English. The actual letters vary from locale to locale.
Table 4-7. POSIX character classes
|
Class |
Description |
Expansion |
|---|---|---|
[:alnum:] |
Alphanumeric characters |
[0-9a-zA-Z] |
[:alpha:] |
Alphabetic characters (letters) |
[a-zA-Z] |
[:ascii:] |
7-bit ASCII |
[\x01-\x7F] |
[:blank:] |
Horizontal ... |
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.
Read now
Unlock full access