Identifiers

Problem

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.

Solution

\b[a-z_][0-9a-z_]{0,31}\b
Regex options: Case insensitive
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Discussion

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 \b[a-zA-Z_][0-9a-zA-Z_]{0,31}\b if you want a regex that does not depend on the case insensitivity option.

The two word boundaries \b make sure that we do not match part of a sequence of alphanumeric characters that is more than 32 characters long.

See Also

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.

Get Regular Expressions Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.