A character class is used to represent a set of characters. The following combinations are allowed in describing a character class:
^$()%.[]*+-?
.
%a
%c
%d
%l
%p
%s
%u
%w
%x
%z
%
[set]
-
[%w_]
[_%w]
[0-7]
[0-7%l%-]
The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.
[%a-z]
[a-%%]
[^set]
For all classes represented by single letters (%a, %c, etc.), the corresponding uppercase letter represents the complement of the class. For instance, %S represents all non-space characters.
%S
The definitions of letter, space, and other character groups depend on the current locale. In particular, the class [a-z] may not be equivalent to %l.
[a-z]
A pattern item can be
*
+
?
%n
%bxy
%b()
A pattern is a sequence of pattern items. A '^' at the beginning of a pattern anchors the match at the beginning of the subject string. A '$' at the end of a pattern anchors the match at the end of the subject string. At other positions, '^' and '$' have no special meaning and represent themselves.
^
$
A pattern can contain sub-patterns enclosed in parentheses; they describe captures. When a match succeeds, the substrings of the subject string that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number 1); the character matching "." is captured with number 2, and the part matching "%s*" has number 3.
"(a*(.)%w(%s*))"
"a*(.)%w(%s*)"
%s*
As a special case, the empty capture () captures the current string position (a number). For instance, if we apply the pattern "()aa()" on the string "flaaap", there will be two captures: 3 and 5.
()
"()aa()"
"flaaap"
A pattern cannot contain embedded zeros. Use %z instead.