3

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?

  • probably because C and most C-like languages have identifiers_like_this. Don't use \w if you need a specific meaning like letters – either use POSIX charclasses like [[:alnum:]] or Unicode properties for that, depending on what your regex engine offers and what you need exactly. – amon May 2 '14 at 22:55
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.