does grep regex work differently on mac?

6

2

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 ?

Rmy5

Posted 2018-04-17T23:15:14.040

Reputation: 63

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 – 2018-04-17T23:46:26.930

2Well, 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! – Gordon Davisson – 2018-04-17T23:56:52.543

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 – 2018-04-18T00:15:09.073

@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 – 2018-04-18T00:31:09.180

@Rmy5 ah maybe -P isn't on Mac! (unless perhaps you can get GNU grep on your mac). It enables for example positive lookahead like (?=abc) -P, --perl-regexp PATTERN is a Perl regular expression (dunno if -P is PCRE or Perl regex) but seems to be a GNU thing. https://apple.stackexchange.com/questions/193288/how-to-install-and-use-gnu-grep-in-osx after installing it you should be able to run it as ggrep apparently

– barlop – 2018-04-18T00:36:02.260

@dsstorefile Please make your comment an answer. – JakeGould – 2018-04-18T00:45:27.517

1@dsstorefile If you answer a question via comments, your comments deserve to be an answer so you can earn appropriate reputation. – JakeGould – 2018-04-18T01:22:54.653

if you happen to use silverarrow you can grep for file names with ag -g '^[a-z]+\.[a-z]{3}$'. – ccpizza – 2019-04-09T13:34:13.617

Answers

7

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

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

user873319

Posted 2018-04-17T23:15:14.040

Reputation:

1And 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 – 2018-04-18T04:27:50.810

3Having 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 – 2018-04-18T04:29:56.410