13

Just trying to figure out basic use of regexes with grep (or egrep) in mac terminal (BSD grep - 2.5.1-FreeBSD).

File to examine (pow.txt) contains the lines :

kiytytytyty

and

blob.mkv

command used is :

grep -E ^[a-z]+\.[a-z]{3}$i pow.txt

match returned is:

kiytytytyty

Obviously this wouldn't match with a PCRE regex. Are regexes interpreted differently on mac ? Or is my syntax wrong ?

8
  • 1
    @dsstorefile Hi. I see, the regex has to be set between quotation marks. AND the dot had to be escaped, AND case insensitive flag has to be placed as part of command params. :) Thanks ! I guess this closes the topic.
    – Rmy5
    Apr 17, 2018 at 23:46
  • 2
    Well, you don't have to quote it; you could also escape all of the characters that have special meaning to the shell. But there's a bunch of them and it's easy to get the escaping wrong; single-quotes are simpler. BTW, instead of "\.", you could use "[.]" to match a period character, and you could use [a-zA-Z] for the character classes instead of the -i option to grep. There's lots of ways to do it! Apr 17, 2018 at 23:56
  • Can you elaborate on "Obviously this wouldn't match with a PCRE regex" By the way there is -P instead of -E. -E is ERE, (which is better than BRE), but -P is more even than ERE. It's Perl Compatible Regular Expression, i.e.(I suppose!) PCRE. Also, the fact that dot would match any character.. and [.] would match a literal dot. is not a PCRE specific thing.
    – barlop
    Apr 18, 2018 at 0:15
  • @barlop yes totally right about the dot, where the problem came from. I thought the problem was differences between ERE and PCRE. BTW, -P doesn't seem to work on my command (=> usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ]), do you have to install it ?
    – Rmy5
    Apr 18, 2018 at 0:31
  • 1
    @dsstorefile If you answer a question via comments, your comments deserve to be an answer so you can earn appropriate reputation. Apr 18, 2018 at 1:22

1 Answer 1

16

If you're trying to match blob.mkv, try:

grep -Ei '^[a-z]+\.[a-z]{3}$' pow.txt
2
  • 2
    And no; regex is not interpreted differently on the Mac. But your shell ate the backslash (and probably replaced $i with an empty string unless you have something in this variable) because you didn't quote the regex.
    – tripleee
    Apr 18, 2018 at 4:27
  • 8
    Having said that, PCRE syntax is different from the regex dialect supported by grep -E on every platform, not just on the Mac. GNU grep has grep -P for PCRE syntax; it used to exist on the Mac, too, but was dropped for reasons only Apple can explain. Perhaps read up on BRE vs ERE vs PCRE. Briefly, \s \w \d and friends, lookarounds, named groups, atomic matching, and non-greedy quantifiers are some of the features only present in PCRE.
    – tripleee
    Apr 18, 2018 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.