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?

  • 1
    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, 2014 at 22:55

1 Answer 1


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 and acknowledge you have read our privacy policy.

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