Pattern: File path

“File path” is one of the patterns that you can select on the Match panel. With this pattern you can match a file path on a Windows or UNIX file system. Linux and macOS use UNIX file systems.

One handy aspect of the “file path” pattern is that it accepts a file path in any of the file path flavors that RegexMagic supports. This flavor can be a different one than the regex flavor used by the application you have selected on the Regex panel. RegexMagic automatically converts the file path from the flavor you specified in the pattern to the flavor selected on the Regex panel.

This example shows how to generate a regular expression that checks whether a string contains a valid absolute path on a local Windows drive or a Windows network share. You can find this example as “Pattern: file path” 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:
    c:
    c:\
    c:\folder
    c:\folder\
    c:\file.txt
    c:\folder1\folder2
    c:\folder1\folder2\
    c:\folder1\folder2\file.txt
    c:\folder name\file name.txt
    \\SERVER\SHARE
    \\SERVER\SHARE\
    \\SERVER\SHARE\folder
    \\SERVER\SHARE\folder\
    \\SERVER\SHARE\file.txt
    \\SERVER\SHARE\folder1\folder2
    \\SERVER\SHARE\folder1\folder2\
    \\SERVER\SHARE\folder1\folder2\file.txt
    \\SERVER\SHARE\folder name\file name.txt
  3. Set the subject scope to “line by line” so we can easily test the regular expression on many file paths.
  4. On the Match panel, set “begin regex match at” to “start of text”, and set “end regex match at” to “end of text”.
  5. Click the Add First Field button to add field 1.
  6. In the “pattern to match field” drop-down list, select “file path”. The default settings allow for any Windows path that starts with a drive letter or network share and ends with a 3-letter extension.



  7. Set the “field validation mode” to “strict”.
  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 you’ll get this regular expression:
    \A(?:[a-z]:|\\\\[@%._~a-z0-9-]{1,63}\\[$@%._~a-z0-9-]{1,80})\\(?:(?!\.+\\)[^\n\r"*<>?|:\\/]+\\)*[^\n\r"*<>?|:\\/]+\.[]!#$%&'()+,0-9;=@\[_`a-z{}~^-]{1,3}\Z
    Required options: Case insensitive; Exact spacing.
    Unused options: Dot doesn’t match line breaks; ^$ don’t match at line breaks.
  9. The Samples panel now highlights the absolute paths in our list:
    c:
    c:\
    c:\folder
    c:\folder\
    c:\file.txt
    c:\folder1\folder2
    c:\folder1\folder2\
    c:\folder1\folder2\file.txt
    c:\folder name\file name.txt
    \\SERVER\SHARE
    \\SERVER\SHARE\
    \\SERVER\SHARE\folder
    \\SERVER\SHARE\folder\
    \\SERVER\SHARE\file.txt
    \\SERVER\SHARE\folder1\folder2
    \\SERVER\SHARE\folder1\folder2\
    \\SERVER\SHARE\folder1\folder2\file.txt
    \\SERVER\SHARE\folder name\file name.txt

Reference