Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does underscore comes under \w?

Tags:

regex

This may be a theoretical question.

Why does underscore _ comes under \w in regex and not under \W

I hope this isn't primarily opinion based, because there should be a reason.

Citation would be great, if at all available.

like image 538
Amit Joki Avatar asked Mar 26 '14 04:03

Amit Joki


1 Answers

From Wikipedia's Regular expression article (emphasis mine):

An additional non-POSIX class understood by some tools is [:word:], which is usually defined as [:alnum:] plus underscore. This reflects the fact that in many programming languages these are the characters that may be used in identifiers.

In perl, tcl and vim, this non-standard class is represented by \w (and characters outside this class are represented by \W).

like image 78
Johnsyweb Avatar answered Oct 27 '22 05:10

Johnsyweb