Skip to content
Advertisement

Does hyphen need to be escaped in a regular expression?

The following returns a bunch of results including those containing “->emit”

grep -rnw '/var/www/react' -e ">emit"

But the following returns no results

grep -rnw '/var/www/react' -e "->emit"

Why?

Advertisement

Answer

You need to drop the -w option. It says to treat the expression as a whole word. A word must be bounded by non-word characters, i.e. it must only be surrounded by characters other than [a-zA-Z0-9_].

emit would match emit, _emit, or ->emit, but not remit.

I’m guessing that your ->emit is trying to match something like a->emit, but can’t because a is a word character.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement