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


Let's examine a few scenarios for replacing placeholders in an archive in 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}}.


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


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:


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


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:



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


And that gives the desired result:


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.*



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



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:




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:




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?
1 out of 1 found this helpful



Please sign in to leave a comment.