Introduction:
Recently I have explored many XPath functions and REGEX functions to ease my development efforts in CPI. I was amazed to see Xpath and Regex can make life so much easier for routing and filtering conditions specifically.
In the below blogs we will see Basics and few functions which I have explored in CPI and how to use them. I have tried to incorporate real business case also wherever possible.
WHAT IS REGEX:
A regular expression (regex) is a sequence of characters that define a search pattern
Basics of REGEX:
The following are some of the most used regex operators:
- `.` (dot): Matches any single character, except for a newline character.
- `+` (plus): Matches one or more occurrences of the preceding character or character class.
- `*` (asterisk): Matches zero or more occurrences of the preceding character or character class.
- `/` (forward slash): Used to separate different parts of a regex expression.
- `^` (caret): Matches the beginning of a string.
- `$` (dollar sign): Matches the end of a string.
- `|` (pipe): Used to separate different alternatives in a regex expression.
- `()` (parentheses): Used to group characters together and create subexpressions.
- `[]` (brackets): Used to create character classes, which match any character within the brackets.
Here are some examples of how these operators can be used:
- `a.b` will match any three-character string that starts with `a` and ends with `b`.
- `a+b` will match any string that starts with `a` and ends with `b`, and has one or more `a` characters in between.
- `a*b` will match any string that starts with `a` and ends with `b`, and can have any number of `a` characters in between, including zero.
- `a/b/c` will match any string that contains the characters `a`, `b`, and `c`, in that order, separated by forward slashes.
Regex operators can be combined to create more complex expressions. For example, the following expression will match any string that contains at least one alphabetical character and at least one numerical character:
[a-zA-Z]+\d+
This expression can be broken down as follows:
- `[a-zA-Z]`: Matches any alphabetical character.
- `+`: Matches one or more of the preceding character.
- `\d+`: Matches one or more numerical characters.
- ‘\w+’ : Matches one or more alphanumerical characters
Regex operators can be a powerful tool for extracting and manipulating data from text strings.
REGEX in CPI with XPATH for Routers and Filters
Payload used:
<whatson> <productions><production>
<category>Film</category>
<price>100</price>
</production>
<production>
<category>Business</category>
<price>1000</price>
</production>
<production>
<category>Business training</category>
<price>free</price>
</production>
</productions>
</whatson>
IFLOW: I used the below flow to simulate the conditions.
Different XPATH REGEX for Routing/Filter condition:
XML Based Condition
Matches function is used in xpath for matching regex.
Following examples illustrates different ways you can use regex in XPATH
//production[matches(price,'[a-zA-Z]')]
– Matches records where price is strickly alphabets.
//production[matches(price,'\d+')]
– Matches records where price is strickly numeric.
//production[matches(price,'\w+')]
- matches records where price is strickly alphanumeric.
//production[matches(category,'Business.+')]
– Matches records which having one or more characters after Business
//production[matches(price,'1\d\d')]
– Matches records have price starting with ‘1’ and two digit after that (Typically useful for match http return codes 2xx , 3xx and 5xx or 4xx series )
//production[matches(category,'Film|Business')]
– Matches record having Film and Business in category
Other XPATH Functions for CPI in Content Modifier
string-join(//price,'_') :
substring(//category,1,4)
replace(//category,'Bus','XYZ')
concat(//category,'|',//price)
tokenize(//category,' ')
contains(//category,'Film')
substring-after(//category,' ')
substring-before(//category,' ')
format-number(//price,'#,##0.00')
For NON-XML Condition
In Router Regex basic remains the same, but one particular use case which I am fond off in CPI is using flags to control flow or control logging. Normally this is done using content modifier which is externalized to sets like ‘Yes/No’ , ‘true/false’ or ‘X/ ‘ , but a user may tend to use any as normally it is not specified and it may not have to be case sensitive for that we can use routing condition
${property.isManualRun} regex '(?i)(true|yes|x)'
?i : is used to ignore Case sensitivity.
Thank You. Hope this helps you in your upcoming assignments