Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
khusal810
Active Participant

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

  1. //production[matches(price,'[a-zA-Z]')] – Matches records where price is strickly alphabets.

  2. //production[matches(price,'\d+')] – Matches records where price is strickly numeric.

  3. //production[matches(price,'\w+')] - matches records where price is strickly alphanumeric.

  4. //production[matches(category,'Business.+')] – Matches records which having one or more characters after Business

  5. //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 )

  6. //production[matches(category,'Film|Business')] – Matches record having Film and Business in category


 

 

Other XPATH Functions for CPI in Content Modifier



  1. string-join(//price,'_') :

  2. substring(//category,1,4)

  3. replace(//category,'Bus','XYZ')

  4. concat(//category,'|',//price)

  5. tokenize(//category,' ')

  6. contains(//category,'Film')

  7. substring-after(//category,' ')

  8. substring-before(//category,' ')

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

 
4 Comments
Labels in this area