Skip to content
Advertisement

not sure what’s the problem with this “regex” in the “grep” shell command

This was my attempt:

grep -i 'th[D]{1,}'

I’m on hackerRank. I have this file that is delivered to my stdin.

I need as a result lines that contains :

the
that
then
those

Advertisement

Answer

In a POSIX BRE pattern, when you use a “shorthand character class” like s, w, even in GNU grep, these escape sequences are treated as separate chars, a backslash and a letter. [D]{1,} matches one or more D letters or chars.

You need to match exactly the words you are told to:

grep -i -E '<(the|that|then|those)>'

Here,

  • -i – enables case insensitive matching
  • -E – enables POSIX ERE syntax
  • < – open word boundary
  • (the|that|then|those) – s capturing group matching any of the word in between | (OR) alternation operators
  • > – close word boundary.
Advertisement