Difference between grep and perl regex?

I have a problem with what I consider the difference in grep and perl regex regex. Consider the following small test:

$ cat testfile.txt 
A line of text
SOME_RULE = $(BIN)
Another line of text

$ grep "SOME_RULE\s*=\s*\$(BIN)" testfile.txt 
SOME_RULE = $(BIN)

$ perl -p -e "s/SOME_RULE\s*=\s*\$(BIN)/Hello/g" testfile.txt
A line of text
SOME_RULE = $(BIN)
Another line of text

As you can see, using the regular expression "SOME_RULE \ s * = \ s * \ $ (BIN)", grep could find a match, but perl could not update the file using the same expression. How do I solve this problem?

Thank you for reading and in advance for any help, rate it!

+5
source share
4 answers

Perl wants '(' and ')' to be escaped. In addition, the shell eats "\" at "$", so you need to:

$ perl -p -e "s / SOME_RULE \ s * = \ s * \\ $ \ (BIN \) / Hello / g" testfile.txt

( - .)

+4

( ) ( ).

perl -p -e 's/SOME_RULE\s*=\s*\$\(BIN\)/Hello/g' testfile.txt

Extended Regular Expression ( ERE):

grep -E "SOME_RULE\s*=\s*\$\(BIN\)" testfile.txt
+2
perl -ne '(/SOME_RULE\s*?=\s*?\$\(BIN\)/) && print' testfile.txt

perl -pe 's/SOME_RULE\s*?=\s*?\$\(BIN\)/Hello/' testfile.txt
+1

regex Perl POSIX, grep. , Perl, - .

, Perl:

s/SOME_RULE\s*=\s*\$\(BIN\)/Hello/g

.

0