Using Regular Expressions in Switch Conditions
When working with Switch containers (see also Switch), you can optionally create a condition within the Switch container to redirect the text fragment if it contains a regular expression match. In such cases, you can use the the caret ( ^ ) and the dollar sign ( \$ ) characters to match the beginning or end of the text to be searched, except when C++ is set as target transformation language. (In C++, the caret and dollar sign characters are interpreted as the beginning or end of a line, not as the beginning or end of the whole text).
To use a regular expression in a switch condition:
1.Define the container type as Switch (click on the top-right corner of the container, and then click Switch).
2.Click the Append Condition ( ) button to add a new condition.
3.Set the condition type to Content contains regular expression match and enter the regular expression in the adjacent text box.
Example
Let's assume that you need to map data from the database log file represented below (you can also find this file at the following path: <Documents>\Altova\MapForce2024\MapForceExamples\Tutorial\SampleDatabaseLog.txt).
Action 18:11:51: INSTALL. Action start 18:11:51: INSTALL. Action 18:11:51: WindowsFolder.68B7C6D9_1DF2_54C1_FF1F_C8B3B9A1E18E. Action start 18:11:51: WindowsFolder.68B7C6D9_1DF2_54C1_FF1F_C8B3B9A1E18E. Action ended 18:11:51: WindowsFolder.68B7C6D9_1DF2_54C1_FF1F_C8B3B9A1E18E. Return value 1. Action 18:11:51: SystemFolder.68B7C6D9_1DF2_54C1_FF1F_C8B3B9A1E18E. Action start 18:11:51: SystemFolder.68B7C6D9_1DF2_54C1_FF1F_C8B3B9A1E18E. Action ended 18:11:51: SystemFolder.68B7C6D9_1DF2_54C1_FF1F_C8B3B9A1E18E. Return value 0. Action 18:11:51: WindowsFolder.9BAE13A2_E7AF_D6C3_FF1F_C8B3B9A1E18E. Action start 18:11:51: WindowsFolder.9BAE13A2_E7AF_D6C3_FF1F_C8B3B9A1E18E. Action ended 18:11:51: WindowsFolder.9BAE13A2_E7AF_D6C3_FF1F_C8B3B9A1E18E. Return value 0. Action 18:11:51: SystemFolder.9BAE13A2_E7AF_D6C3_FF1F_C8B3B9A1E18E. Action start 18:11:51: SystemFolder.9BAE13A2_E7AF_D6C3_FF1F_C8B3B9A1E18E. Action ended 18:11:51: SystemFolder.9BAE13A2_E7AF_D6C3_FF1F_C8B3B9A1E18E. Return value 1. Action 18:11:51: WindowsFolder.63E949F6_03BC_5C40_FF1F_C8B3B9A1E18E. Action start 18:11:51: WindowsFolder.63E949F6_03BC_5C40_FF1F_C8B3B9A1E18E. Action ended 18:11:51: WindowsFolder.63E949F6_03BC_5C40_FF1F_C8B3B9A1E18E. Return value 1. |
Your goals are as follows:
1.Collect in a list all rows where a return value is present. That is, the list must include every row which contains the value "Return value 1" or the value "Return value 0", or any other return value expressed as a digit.
2.Collect in another list all rows where the text contains the value "Action start".
To achieve these goals, you can use a Repeated Split container to split down the text into individual rows. After that, you can use a Switch container to redirect each row as required. Namely, the Switch container will consist of three conditions, as follows:
1.Redirect the current row to output A if it contains the value "Action started". You can find such rows by using a condition of type "Content starts with", and enter "Action started" as value.
2.Redirect the current row to output B if it contains a return value. You can find such rows by using the regular expression Return value [0-9]\. . This regular expression will return a match if the row contains the text "Return value", followed by a single digit, followed by a full stop. The backslash ( \ ) before the full stop acts as an escape character, to denote that the full stop must be treated as normal character, not as a metacharacter.
3.Redirect the current row to output C (<default>) if the row satisfies neither of the conditions above.
To create the FlexText template which performs the tasks above:
1.On the Insert menu, click Text File.
2.Click Design graphically new structure based on a sample file and save the FlexText .mft template to a directory of your choice.
3.When prompted to open a text file, browse for the <Documents>\Altova\MapForce2024\MapForceExamples\Tutorial\SampleDatabaseLog.txt file.
4.Click the top-right corner of the output container and select Repeated Split. Because we are using carriage return as split character, choose delimited (floating) mode, and <CR><LF> as separator. This creates a new output container which consists of 17 blocks (one for each row).
5.Click the top-right corner of the new output container and select Switch. Now FlexText will treat the contents of the container as a switch. As shown below, one <default> switch condition was created automatically—this condition redirects to a new container any text that does not match other conditions. At this stage, there are no other conditions defined, therefore all text is currently being redirected to the <default> output.
6.Click the Append Condition ( ) button and add the condition of type Content starts with with the value "Action start", as shown below. This condition redirects to a new container any text that begins with "Action start".
7.Click the Append Condition ( ) button and add the condition of type Content contains regular expression match with the value Return value [0-9]\. . If you now navigate to block 5 out of 17, you can see that this condition redirects that block to a new container, since the block contains a match for the regular expression.
You have now configured the Switch container so that it redirects text to a different output based on a conditions. There are three switch conditions and three possible outputs (one output for each condition). The remaining steps of this tutorial show how you can write each of output to a separate text file.
8.Click the top-right corner of each output container and choose Store as Value. Additionally, double-click the title bar of each output and enter a descriptive name: Output A, Output B, and Output C, respectively.
9.Save and close the FlexText template.
The required FlexText template has now been created. You can see how this template works by opening the following tutorial file: <Documents>\Altova\MapForce2024\MapForceExamples\Tutorial\FlexTextSwitchByRegEx.mfd.
FlexTextSwitchByRegEx.mfd
The FlexTextSwitchByRegEx.mfd mapping illustrated in the screen shot reads text data from the <Documents>\Altova\MapForce2024\MapForceExamples\Tutorial\SampleDatabaseLog.txt file and converts it into three separate text files: Output A, Output B, and Output C. Each of the target text files contains only the rows that satisfy one of the three conditions defined previously in the FlexText template. To view the output of a particular target component, click the Preview Component ( ) button in the top-right corner of the component, and then click the Output tab.