tags:

views:

608

answers:

5

What does the underscore mean in the following regex?

[a-zA-Z0-9_]

The _ seems to make no difference so I don't understand the purpose of it.

+10  A: 

It means the underscore is also matched.

Otávio Décio
+44  A: 

The underscore means an underscore.

ndim
Although, I wish it had been phrased "underscore is underscore" :-)
pst
Try as a might, I couldn't find a single problem with your statement; it's utter simplicity and logic is beyond reproach :-) +1
paxdiablo
And never before was the rep cap so easily hit...
Ether
I just gave you a populist badge, by up-voting the accepted answer.
Brad Gilbert
+24  A: 

It means to match the underscore character in addition to lowercase letters, uppercase letters, and numbers.

No Surprises
Your name is especially fitting for this question. Well done.
Telemachus
+7  A: 

With the exception of character sequences ([., [:, and [=), range expressions (e.g., [a-z]), and the circumflex in the beginning ([^), every character inside a bracket expression means the character itself, just like that underscore.

As a side note, that expression is commonly represented by \w (word character, ignoring unicode and locale), and is commonly used to define the set of characters that are allowed to be used in variable names.

JG
\w depends on locale and also will match Unicode characters
Alexandr Ciornii
For most languages, `[a-zA-Z_][a-zA-Z0-9_]*` is probably more accurate for variable names since they prohibit starting with digits.
Tim Hatch
And many languages nowadays allow other scripts for variable names as well. So Have fun with Unicode classes as well :-)
Joey
+9  A: 

Regular expressions are documented in perlre. That's the place to check whenever you have a question about regular expressions. The Regular-Expressions.info site is very helpful too.

To get you started, the thing you are looking at is called a "character class".

brian d foy
+1 for answering *more* than the immediate question.
paxdiablo
Thank you. Only if all of us were so tolerant of the newbies.
kunjaan
For someone new to regular expressions in Perl, I always recommend `perldoc perlrequick` first. It's a lot more friendly than `perldoc perlre` - mostly because it's not so overwhelmingly detailed.
Telemachus