Linux (6) OpenCV (4) MATLAB (3) Mac OS X (3) Windows (2) C# (1) Node JS (1)

2013年6月25日 星期二

Convert File Glob to Regular Expression

The tips of converting file glob to regular expression are extracted from following website
Thanks to Jan Borsodi for the good tutorial. The tips are as follows:

For people who have some knowledge with wildcards (also known as file globs or file globbing), I'll give a brief explanation on how to convert them to regular expressions. After reading this article, you probably have seen the similarities with wildcards. For instance:

matches any text which end with .jpg. You can also specify brackets with characters, as in:

matches any text which ends in .cpp or .hpp. Altogether very similar to regular expressions.
The * means match zero or more of anything in wildcards. As we learned, we do this is regular expression with the punctuation mark and the * quantifier. This gives:

Also remember to convert any punctuation marks from wildcards to be backslashified.
The ? means match any character but do match something. This is exactly what the punctuation mark does.
Square brackets can be used untouched since they have the same meaning going from wildcards to regular expressions.
These leaves us with:
  • Replace any * characters with .*
  • Replace any ? characters with .
  • Leave square brackets as they are.
  • Replace any characters which are metacharacters with a backslashified version.

would be converted to:


would convert to:

or alternatively: