You need a regular expression that matches any identifier in your source code. Your programming language requires identifiers to start with an underscore or an ASCII letter. The following characters can be underscores or ASCII letters or digits. Identifiers can be between 1 and 32 characters long.
[a-z_][0-9a-z_]{0,31}
Regex options: Case insensitive |
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
The character class ‹[a-z_]
› matches the first character in the
identifier. ‹[0-9a-z_]
›
matches the second and following characters. We allow between 0 and 31
of those. We use ‹[0-9a-z_]
› rather than the shorthand ‹w
› so we don’t need to worry
whether ‹w
› includes
non-ASCII characters or not. We don’t include the uppercase letters in
the character classes, because turning on the case insensitive option
does the same and usually requires fewer keystrokes. You can use
‹[a-zA-Z_][0-9a-zA-Z_]{0,31}
› if you want a
regex that does not depend on the case insensitivity option.
The two word boundaries ‹› make
sure that we do not match part of a sequence of alphanumeric characters
that is more than 32 characters long.
Techniques used in the regular expressions in this recipe are discussed in Chapter 2. Recipe 2.3 explains character classes. Recipe 2.6 explains word boundaries.