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. For example, the metasequence \. matches the period character.
> (regexp-match #rx"p.t" "pet")
The above pattern 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. Thus, #rx"p[aeiou]t" matches pat, pet, pit, pot, put, and nothing else.
Inside the brackets, a - between two characters specifies the Unicode range between the characters. For example, #rx"ta[b-dgn-p]" matches tab, tac, tad, tag, tan, tao, and tap.
An initial ^ after the left bracket inverts the set specified by the rest of the contents; i.e., it specifies the set of characters other than those identified in the brackets. For example, #rx"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. A - is a metacharacter only when it’s inside brackets, and when it is neither the first nor the last character between the brackets.
Bracketed character classes cannot contain other bracketed character classes (although they contain certain other types of character classes; see below). Thus, a [ inside a bracketed character class doesn’t have to be a metacharacter; it can stand for itself. For example, #rx"[a[b]" matches a, [, and b.
Furthermore, since empty bracketed character classes are disallowed, a ] immediately occurring after the opening left bracket also doesn’t need to be a metacharacter. For example, #rx"ab]" matches ], a, and b.
In #px syntax, some standard character classes can be conveniently represented as metasequences instead of as explicit bracketed expressions: \d matches a digit (the same as [0-9]); \s matches an ASCII whitespace character; and \w matches a character that could be part of a “word”.
Following regexp custom, we identify “word” characters as [A-Za-z0-9_], although these are too restrictive for what a Racketeer might consider a “word.”
The upper-case versions of these metasequences stand for the inversions of the corresponding character classes: \D matches a non-digit, \S a non-whitespace character, and \W a non-“word” character.
Remember to include a double backslash when putting these metasequences in a Racket string:
> (regexp-match #px"\\d\\d" "0 dear, 1 have 2 read catch 22 before 9")
These character classes can be used inside a bracketed expression. For example, #px"[a-z\\d]" matches a lower-case letter or a digit.
A POSIX character class is a special metasequence of the form [:...:] that can be used only inside a bracketed expression in #px syntax. The POSIX classes supported are
ASCII letters and digits
ASCII widthful whitespace: space and tab
“control” characters: ASCII 0 to 32
ASCII digits, same as \d
ASCII characters that use ink
ASCII lower-case letters
ASCII ink-users plus widthful whitespace
ASCII whitespace, same as \s
ASCII upper-case letters
ASCII letters and _, same as \w
ASCII hex digits
For example, the #px"[[:alpha:]_]" matches a letter or underscore.
> (regexp-match #px"[[:alpha:]_]" "--x--")
> (regexp-match #px"[[:alpha:]_]" "--_--")
> (regexp-match #px"[[:alpha:]_]" "--:--")
The POSIX class notation is valid only inside a bracketed expression. For instance, [:alpha:], when not inside a bracketed expression, will not be read as the letter class. Rather, it is (from previous principles) the character class containing the characters :, a, l, p, h.
> (regexp-match #px"[:alpha:]" "--a--")
> (regexp-match #px"[:alpha:]" "--x--")