Sunday, August 26, 2012

Regular expressions vs shell patterns

Today I have discovered an important difference between regular expressions and shell patterns in a context of file searching in Linux. Their both applications may be found in a powerful utility find. Its arguments -name, -path and -wholename are interpreted as shell patterns, and -regex - as regular expressions.
The difference between regular expressions and shell patterns is in how some characters are interpreted.
Let's consider a star character (*). In shell pattern it is interpreted as match of zero or more characters. In regular expression is interpreted differently: it matches preceding character or group zero or more times.
For example, the string '*tmp' will match all strings ending with tmp by shell pattern processor, but regular expression processor won't match anything at all! In order to write an equivalent regular expression of a shell pattern we have to write '.*tmp'.
These commands are equivalent:
find . -name '*tmp'
find . -regex '.*tmp'
So be careful when writing matching expressions and know how it is interpreted.

No comments:

Post a Comment