Simple Regular Expression (PCRE) Matching
The expression language supports simple matching on regular expressions using the ~= operator and its variations. With these operators, the right hand side is interpreted as a PCRE (Perl-Compatible Regular Expression).
Regular expressions offer far more powerful pattern matching capabilities than globs, but are generally less efficient (even in relatively simple forms) and can be harder to read. The use of globs is preferred in cases where the glob match functionality can meet the requirements.
This document does not describe the PCRE syntax in any detail. For more information, refer to the PCRE documentation.
Some common pitfalls of using regular expressions:
Regular expressions are not anchored by default. That means that unless explicitly anchored, a regular expression can match anywhere in the subject string (the left hand side of the expression) where a match is possible. For example, the regular expression foo matches “foo”, “foofoo”, “some computer engineers use ‘foo’ a lot but no one knows what it means”, etc.
There are many special characters in regular expressions, far more than there are in globs. This includes dot (“.”), which matches any character. Therefore, the regular expression foo.html matches “foo.html” and “fooFhtml”, etc.
A star (“*”) in a regular expression does not mean the same as it does in a glob. Rather, it modifies the meaning of the prior match to mean “match any number of times, including 0”. So f* matches “f”, and also matches “fff” (anywhere in the subject string). It also matches zero occurrences of “f”. This means that even if the subject string has no “f”s at all, f* will match it.
A common case where a regular expression match is useful is in alternation, that is, matching two or more alternate sequences of characters. For example, the following is true if the path ends in “.jpeg”, “.jpg”, or “.gif”: