简体   繁体   中英

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?

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 ( az ) 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM