... in Templates |
Top Previous Next |
The Bat! allows full access to the power of regular expressions in message templates. The general idea is to search some text for the first occurrence of a sub-string matching a pattern defined in a regular expression and then to insert the result of that search right back into the message text. For example, it is possible to parse incoming messages of a particular format and create new messages in another format with extra, modified or removed information.
To search the original message text for a sub-string, use the %REGEXPTEXT="regexp" macro where regexp defines the search pattern. The %REGEXPQUOTES="regexp" macro does the same, but returns the result pre-formatted as a quotation.
When you need to search any generic text for a particular sub-string, use the %SETPATTREGEXP="regexp" and %REGEXPMATCH="string" macro combination. regexp defines the search pattern for all subsequent occurrences of the %REGEXPMATCH macro until the next instance of %SETPATTREGEXP. Since the string parameter is within a template, it is possible to use any of the available macros to generate any text you need, e.g. %QUOTES="%SETREGEXP=""regexp""%REGEXPMATCH=""%TEXT""" is equivalent to %REGEXPQUOTES="regexp".
If a match is found, the text that matches a given regular expression is returned. If the regular expression has subpatterns, the first captured substring is returned, i.e for the regular expression Total amount: *(\S+) only the word following Total amount will be returned but not Total amount itself.
%REGEXPBLINDMATCH="string" is like the %REGEXPMATCH="string" macro except that matched string/substrings are not returned, but may be further extracted using %SUBPATT="n" macros where n is the number of a captured substring; %SUBPATT='0' returns the text matching the entire Regular Expression pattern, 1 is the text of the first matching subpattern, 2 - the second matching subpattern,. etc... Thus the %REGEXPBLINDMATCH="string" macro allows you to have more then one subpattern in its results e.g.
%QUOTES="%SETPATTREGEXP=""total_amount +(\S+).*flowers_type +(\S+)""%REGEXPBLINDMATCH=""%TEXT""pay %SUBPATT=""1"" for %SUBPATT=""2"""
The default PCRE options for templates are PCRE_CASELESS+PCRE_MULTILINE+ PCRE_DOTALL. Matching is done by treating a whole message as one string. CF/LF pairs (0D/0A) are replaced with single LF (0A) characters before matching.
See also: Regular Expressions Syntax (Basic). |