Ray
Ray

Reputation: 6105

backslashes in regular expressions

Someone asked how to find all files of certain file types, and one of the answers was:

 find /path/to -regex ".*\.\(jpg\|gif\|png\|jpeg\)" > log

I'm trying to understand the backslashes better. The first backslash is saying to take the period as literal, not as pattern matching, is that right? If so, why is the second backslash needed, and the third? It seems like the second backslash is saying take the ( as a literal character.

Upvotes: 1

Views: 48

Answers (1)

anubhava
anubhava

Reputation: 785226

\( and \| are just default regex (emacs) syntax which requires parentheses and alternation | to be escaped. You can use an advanced regex type and avoid excessive escaping like this:

find /path/to -regextype posix-egrep -regex ".*\.(jpg|gif|png|jpeg)" > log

As per man find:

-regextype type

Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. Currently-implemented types are emacs (this is the default), posix-awk, posix-basic, posix-egrep and posix-extended.

Upvotes: 2

Related Questions