How to: Use Regex to Control Placeholder Replacement in Text Files When Deploying Archives in Digital.ai Deploy

Let's examine a few scenarios for replacing placeholders in an archive in Digital.ai Deploy. These apply to any archive in ZIP or TAR format, including EARs, JARs, WARs, and many other common formats.

Deploy can scan text files for placeholders and replace them during a deployment. Text files are identified by their file extension such as .cfg, .txt, and so on. The default list of known text extensions can be expanded to include new formats.

Placeholders within text files delimited by double opening and closing braces will be recognized by Deploy's deployment engine and replaced with values provided by Dictionaries or by the user in the GUI. The delimiters for a given type can be changed if the {{...}} notation conflicts with other usage.

Finally, a regex string can be provided to exclude those directories or files that match the regex. The matching is done against the path within the archive.

Here is the structure of a zip file, with directories named after well-known (astronomical) stars, and configuration files named after colors. All the files have the same content: {{PLACEHOLDER}}.

1-Zip_structure.png

We can get a quick glimpse of all the source file contents with a quick and dirty shell loop:

for FILE in *.cfg */*.cfg */*/*.cfg */*/*/*.cfg; do echo $FILE; cat $FILE; 
echo "---------------"; done

2-Source_File_Contents.png

Import the zipped file into Deploy as a file.Archive Deployable. Be sure to check the Replace Placeholders boolean. Deploy will take in the file and add the checksum and placeholders to the configuration item:

1a-config-item.png

Our dictionary will replace PLACEHOLDER with REPLACED in every file except those excluded.

2a-dictionary.png

The first scenario uses a simple regex to exclude the colors that start with 'a':  .*/a.*\.cfg 

Note that we have to account for any pathing that precedes the filename.  But there is a mistake here, as well see shortly:

3a-regex.png

6-Source_File_Contents_2.png

Oops, we've excluded directories that start with 'a' as well. (Remember, the excluded files show up as still containing {{PLACEHOLDER}}.)

Let's add a negation of the forward-slash character after the a, so we won't exclude the directories that start with 'a': .*/a[^/]*\.cfg

4a-regex.png

And that gives the desired result:

8-Source_File_Contents_3.png

Here is an example that excludes everything under a top-level directory starting with 'v', but allows a file starting with v in other directories: v.*

5a-regex.png

10-Source_File_Contents_4.png

If I want to exclude the deneb/sirius directory and everything under it, I use deneb/sirius/.*

6a-regex.png

12-Source_File_Contents_5.png

Finally, let's look at a use case that requires us to reverse the logic. How would we exclude everything except a particular config file? Let pick vega/polaris/aqua.cfg as the only file where we want the replacement to happen.

Regex allows a lookahead feature, where you can match on a portion of a string if it's followed by other text, but the other text is not considered part of the match. The classic example is q(?=u), which is a directive to match 'q' when it is followed by 'u', but the 'u' is not part of the matched text.

A negative lookahead is q(?!u), which matches 'q' when not followed by 'u', and the letter following 'q' is not considered part of the match.

So our regex here is:

^(?!vega/polaris.aqua.cfg).*

7a-regex.png

14-Source_File_Contents_6.png

If we want to add deneb/sirius/arcturus/azure.cfg to be included as well, we add it within the parentheses following a vertical bar for 'or' logic:

^(?!vega/polaris.aqua.cfg|deneb/sirius/arcturus/azure.cfg).*

8a-regex.png

16-Source_File_Contents_7.png

Regex syntax can be arcane but Deploy supports it fully. You can easily search for online tools to help you craft the proper regex to get the matches you want.

 

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.