Regular Expressions – Why Does the \w Metacharacter Include Underscores

regular expressions

In most, if not all implementations of regular expressions, the \w metacharacter matches all alphanumeric characters plus the underscore.

Historically speaking, why was the underscore character included in this character class? And why not include dashes too?

Best Answer

Because underscores are second-nature for identifiers in almost all computer languages that matter. Dashes are not; they're typically used as an operator for subtraction, and are specifically excluded from identifiers.

Related Topic