Fork me on GitHub

Use regular expressions to transform file names

A particularly powerfule file mapper deserves more detailed description: It's the regular expression file mapper.

This file mapper uses regular expressions to identify certain pieces in the input file name. This piece, or these pieces are replaced with a replacement pattern.

Let's first design a configuration snippet, which would transform all files in the directory src/main/xml by applying the stylesheet src/main/stylesheet.xsl. The created files would have the extension .fo, rather than .xml. (We could very well achieve the same by using the FileExtensionMapper, as demonstrated in this example.)

                <fileMapper implementation="">

Let's have a look at some other examples. We omit most of the details, except for the pattern and the replacement, which are the important parts:

    <fileMapper implementation="">

This example is identical with the previous. However, it demonstrates the use of capture groups. The contents of (.*) are captured and inserted by using the group term $1. You might have multiple groups and use the terms $2, $3, and so on.

    <fileMapper implementation="">

This example replaces the first a with a b. Nothing special here, except that you might like to replace all a's instead. Here's how to do it:

    <fileMapper implementation="">