Home » Blog
date 7.Sep.2014

■ How to find files using regular expressions

Regular expressions (abbreviated to regexp) are power charged wildcards that can match anything you can imagine. Like DOS wildcards they reserve some special characters for matching strings — albeit in a more complicated fashion. Whereas DOS wildcards have just two special characters ? and * (that match one and any sequence of letters/numbers respectively), regexps have extended notation that caters for character groups and boolean operators. Here is a small example:
regexp  .*\.txt
Both these match any filename with txt extension, but the regexp version is more complicated: the dot (.) is a special character that matches any single letter or number (like the plain ? in DOS wildcards) so we have to escape it with \. to match the filename extension. So it doesn't pay much to use regexps for simple matching, unless you want to check for something more elaborate.

All search-style commands in xplorer² can accept regular expresssions, both for finding file names and searching in file contents. Let's take for example <ALT+G> command that selects files with matching rules. The Named field normally accepts DOS wildcards but you can turn on the regexp mode if you set the fuzzy percent indicator box to 0 (yes, clearly that was an afterthought :) to search filenames using the regexp syntax. Searching for text inside documents using regexp is more straightforward, you only have to tick the RE option box then specify your regular expression in Containing text.

Textual attributes (Additional rules) like file comments can also be matched if you tick Regular expression box, see the picture to the far right.
regular expression in filenames and contents regular expression in file attributes

Now onto a few clarifications on xplorer² use of regexps. Regular expressions in file names and attributes are case insensitive, which means that ab[12] will match ab1 and AB2 (notice the mixing of upper and lowercase letters). Only when you search for text in file contents you have the option to choose what to do with character case: if you want exact match tick the Case sensitive option.

Regular expressions will match any part of the filename, for example a* will match any file that has one or more "a" in its name. If you want to match exactly the regexp (in full, not partially) add the special symbols that match the beginning ^ and end $ of the string, e.g. ^a*$ will only match files that consist of one or more "a" but not something like abc.

Finally note that xplorer² regexp syntax isn't 100% PERL compatible at present. For the next version I am considering switching to the DEELX regexp engine that proved decent in performance tests and it is closer to the syntax most people would consider "standard".

ps. I used Mark with rule command above as an illustration. In a similar fashion you can use regular expressions in other xplorer² commands, e.g. those that filter and search for files <CTRL+F>, even in color coding and grouping rules.

Post a comment on this topic »

Share |

©2002-2014 ZABKAT, all rights reserved | Privacy policy | Sitemap