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.
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.
It means to match the underscore character in addition to lowercase letters, uppercase letters, and numbers.
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.
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".