Currently, I have a following regex. It should match a string that I am echoing:
echo "TBGFSGFI22800_D_REP_D_RISIKOEINHEIT" | grep -E 'TBGFSGFI\d\d\d\d\d[A-Za-z_]{1,100}'
It works as expected in OsX on my Mac and in Notepad++, but in Bash for windows (MINGW64) I get an empty string. How can I use the grep with flags, or how should I rewrite the regex to match the pattern?
My grep version is 3.1. Bash: 4.4.23(1)
Thanks for help in advance!
You are using a POSIX ERE regex with the -E
option, and that flavor does not support \d
construct. You also need -o
option to actually extract the matches.
Note you do not need to repeat \d
five times, you can use a range quantifier, \d{5}
.
You can use
echo "TBGFSGFI22800_D_REP_D_RISIKOEINHEIT" | grep -Po "TBGFSGFI\d{5}[A-Za-z_]{1,100}"
Where
-P
means the regex is of a PCRE flavor-o
extracts matches onlyTBGFSGFI\d{5}[A-Za-z_]{1,100}
- a regex that matches TBGFSGFI
, then any five digits and then 1-100 ASCII letters or _
.