Pattern: File mask

“File mask” is one of the patterns that you can select on the Match panel. With this pattern you can convert a traditional file mask or globbing expression into a regular expression.

This example shows how to generate a regular expression that produces the same results as a file mask. You can find this example as “Pattern: file mask” in the RegexMagic library.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample:
    test.txt
    test.bak
    test.cpp
    one.dpr
    two.pas
    two.dfm
    
  3. Set the subject scope to “line by line” so we can easily test the regular expression on many file names.
  4. On the Match panel, set “begin regex match at” to “start of text”, and set “end regex match at” to “end of text”. This is essential. Traditional file masks require the whole file name to match, while regular expression engines usually allow partial matches. The file mask *.c, for example, does not match the file name main.cpp because it does not end with the letter c. If your regular expression engine has a function that checks whether a regular expression matches the subject string entirely then you can use that instead of the start and end of text anchors.
  5. Click the Add First Field button to add field 1.
  6. In the “pattern to match field” drop-down list, select “file mask”.



  7. Paste in this file mask:
    *.txt
  8. On the Regex panel, select “Python 3.13” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and RegexMagic converts the file mask into this regular expression:
    \A[^\n\r"*:<>?|]*?\.txt\Z
    Required options: Case insensitive.
    Unused options: Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks.
  9. The Samples panel now highlights the file names that match the file mask:
    test.txt
    test.bak
    test.cpp
    one.dpr
    two.pas
    two.dfm
    

Reference