I'm sure some version of this question has been asked and answered before, but I've looked around and haven't found the exact answer. Perhaps someone here can help the lightbulb go on for me. I'm on a Mac with Mojave 10.14.6 and bash 3.2.57(1)-release.
I'm learning the basics of regular expressions by following along with an online tutorial, and practicing both on the online site https://regexr.com, and by using
grep in bash on my local machine.
I'm practicing with a small text file (called small.txt) with three things in it:
9.00 9-00 9500
I understand that the
/9.00/g will match all three strings
It's the same if I use
grep on the command line:
~/bin $ grep 9.00 small.txt 9.00 9-00 9500
So far, so good. The tutorial says that to turn the
. from a metacharacter into a literal, you have to escape it. Okay. so putting
/9\.00/g into the online regex box will only match
9.00, as expected, not 9-00 nor 9500. Great.
However, if I enter that same syntax into
grep on the command line, I get an unexpected result:
~/bin $ grep 9\.00 small.txt 9.00 9-00 9500
Same as before. To get
grep to work, I either have to double quote the whole string:
~/bin $ grep "9\.00" small.txt 9.00
or just double quote the escaped character:
~/bin $ grep 9"\."00 small.txt 9.00
There may well be some other quoting choices that I could make that would also give me the correct result.
This is making it hard for me to wrap my head around the basics of regular expression, because, clearly, I first have to understand how
grep in the shell differs from traditional regular expression syntax. It's hard enough learning all of the rules for regular expressions, but when you add in the differences between classic regular expression and the behavior of the bash shell, my head explodes.
Anyway, wondering if there was a clear explanation that will clear this up for me and set me on the path to properly learning regular expressions that I can use with grep on the command line.
(None of the courses on regular expression point out the differences between the command line version of grep with bash, and the "pure" regular expression syntax that you see on the online regex testers.) I know that there are differences between engines at the advanced level, but this seems to be something so basic, that I feel that I must be missing something.