Redirections using regular expression (Regex) pattern matching

17/09/2018 Stuart Moore

Redirections and Regular Expressions 

File redirection rules support regular expressions to help reduce the number of redirections required for Containers. The ability to specify wildcards makes it far easier to redirect all of the application data to user's profile location, or away from the Container if you want to deploy read only Containers on your desktops and servers.

Exact matches for files

<ExactMatch>
<From IsRegex="true">c:\\some\\folder.*\\notepad\+\+.config </From>
<To>c:\another\file.txt</To>
<!-- Notice that there are no escaped characters here -->
</ExactMatch>

Folder matching with recursion

The regular expression .* is used for recursive pattern matching, and can be used in redirections to redirect all the files, folders and subfolders from one location, to another.

<FolderMatch>
<From IsRegex="true">c:\\some[.]*</From>
<To>c:\another</To>
</FolderMatch>

Will match the following

C:\sdfaksdjfl\adir
C:\aksdjk\and\adir

Folder matching without recursion

The regular expression [^\\]* is used for non-recursive pattern matching - it will NOT find a match for files or folders in sub folders because no soon as it encounters the path seperator '\' the matching stops.

<FolderMatch>
<From IsRegex="true">c:\\[^\\]*\\adir</From>
<To>c:\data</To>
</FolderMatch>

Will match

C:\sdfaksdjfl\adir
C:\AA\adir

Wont match

C:\aksdjk\another\adir


Common Errors

Special characters in the 'From' tag content must be escaped, in contrast with common redirection rules. The following characters have a special meaning in regular expressions so they must be escaped if the intention is to use them as literal strings, instead of as part of the regular expression: 

[ \ ^ $ . | ? * + ( )


Testing

Always test the expression you're building to ensure that it has the desired matching behaviour. There are a number of online regular expression engines which can be used for this, for example Regexr.

Source:
Was this article helpful?

Table of Contents

    Can't find what you're looking for?

    Contact Support