[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
13.2.2 Characters and character classes
Typically a character in the regexp matches the same character in the
text string. Sometimes it is necessary or convenient to use a regexp
metasequence to refer to a single character. Thus, metasequences
\n
, \r
, \t
, and \.
match the newline,
return, tab and period characters respectively.
The metacharacter period (.
) matches
any character other than newline.
(pregexp-match "p.t" "pet") ⇒ ("pet")
It also matches pat
, pit
, pot
, put
,
and p8t
but not peat
or pfffft
.
A character class matches any one character from a set of
characters. A typical format for this is the bracketed character
class [
...]
, which matches any one character from the
non-empty sequence of characters enclosed within the
brackets.(5) Thus "p[aeiou]t"
matches
pat
, pet
, pit
, pot
, put
and nothing
else.
Inside the brackets, a hyphen (-
) between two characters
specifies the ascii range between the characters. Eg,
"ta[b-dgn-p]"
matches tab
, tac
, tad
,
and tag
, and tan
, tao
, tap
.
An initial caret (^
) after the left bracket inverts the set
specified by the rest of the contents, ie, it specifies the set of
characters other than those identified in the brackets. Eg,
"do[^g]"
matches all three-character sequences starting with
do
except dog
.
Note that the metacharacter ^
inside brackets means something
quite different from what it means outside. Most other metacharacters
(.
, *
, +
, ?
, etc) cease to be metacharacters
when inside brackets, although you may still escape them for peace of
mind. -
is a metacharacter only when it’s inside brackets, and
neither the first nor the last character.
Bracketed character classes cannot contain other bracketed character
classes (although they contain certain other types of character classes
— see below). Thus a left bracket ([
) inside a bracketed
character class doesn’t have to be a metacharacter; it can stand for
itself. Eg, "[a[b]"
matches a
, [
, and b
.
Furthermore, since empty bracketed character classes are disallowed, a
right bracket (]
) immediately occurring after the opening left
bracket also doesn’t need to be a metacharacter. Eg, "[]ab]"
matches ]
, a
, and b
.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.