Regex with grep - matches an unknown number of alphabetic characters?

I am looking for something like:

STRINGONE_****_******_STRINGTWO 

Where do I know STRINGONE and STRINGTWO. However * reflects an unknown number of letters (capital).

I was on an assumption

 grep -nr "STRINGONE_\w+_\w+_STRINGTWO" . 

will work, but I find no match.

How to correctly formulate this regex (using grep in cygwin)?

+4
source share
1 answer

See the manual. Without the -E flag, grep accepts "base" regular expressions. In this case, most metacharacters lose their special meaning if you do not escape them. So that

 grep -nr "STRINGONE_\w\+_\w\+_STRINGTWO" 

or

 grep -nrE "STRINGONE_\w+_\w+_STRINGTWO" 

Or, since you only need uppercase letters:

 grep -nr "STRINGONE_[AZ]\+_[AZ]\+_STRINGTWO" grep -nrE "STRINGONE_[AZ]+_[AZ]+_STRINGTWO" 

Otherwise, strings with three or more components in the middle will also be accepted.

+3
source

Source: https://habr.com/ru/post/1496654/


All Articles