Skip to content
Advertisement

Grep Pattern matching- underline

I’ve not been able to find anything online to help so hoping someone may have an idea.

What does an underline in an expression mean when using grep?

For example: [_a-zA-Z0-9]

Could someone help to explain the purpose here?

Advertisement

Answer

The grep command uses a regular expression as it is also described in the manpage of grep:

A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to arithmetic expressions, by using various operators to combine smaller expressions.

A quick reference of the regular expression syntax can be found here. To test regular expressions with several input strings I recommend regex101.


The pattern [_a-zA-Z0-9] means to match a single character in the list. The list is opened with [ and closed with ]. The underscore (_) has no special meaning it is literally the underscore character. The minus character (-) means range, here from a to z (a-z) for example.

In short [_a-zA-Z0-9] means to match a single character wich is _, a character of the alphabet either lower or uppercase or a numerical character.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement