I have a text file, test that contains the following string
MDXXXXXMDthough in general, X could be any character and M could also be F. I'm trying to select for this string with the following grep command:
grep '\(F\|M\)D.{,5}\(F\|M\)D' testHowever, this does not return anything, meaning that the regex cannot select for the string. The \(F\|M\)D part works fine:
➜ ~ grep '\(F\|M\)D' test
MDXXXXXMDDoesn't .{,5} mean up to 5 occurences of any character? What am I missing?
(I'm on mac if that makes a difference)
12 Answers
On a Mac I would try with {0,5} instead of an empty entry ({,5}). Because the empty entry is a GNU extension which may not be supported on the Mac.
Also, just like with the other regex characters, the { and } should be escaped \{0,5\} should work best.
If the 0 is an issue (invalid count), then you can make it with an extra set of parenthesis:
\(.\{1,5\}\)\?That means those 1 to 5 characters are optional.
1You should be escaping also the brackets:
grep '\(F\|M\)D.\{,5\}\(F\|M\)D' test 3