For some more elaborate and dynamic find/replace functions in TI, I was forced to do some cool stuff a while ago: regular expressions.
While not being exactly regular expressions as per definition, they offer a lot of flexibility and have become my golden hammer in many situations.
It works like this:
- Define your original string
- Define your filter expression
- Define any replacement rules.
Example 1:
Here we do a simple replace. Replace any space characters with underscores.
Code: Select all
vString = 'TM1 is great!';
vExpression = ' ';
vReplacement = '_';
Code: Select all
TM1_is_great!
Example 2:
Here we do not only want to replace, we want to add to a matched string. For this we use the $& to refer to the matched string.
Code: Select all
vString = 'CellPutN(vValue, vCubename, cm_dim1, cm_dim2, cm_dim3)';
vExpression = 'CellPut?(*,*,';
vReplacement = '$& cm_extra, ';
Code: Select all
CellPutN(vValue, vCubename, cm_extra, cm_dim1, cm_dim2, cm_dim3)
Example 3:
When not replacing stuff, sometimes we just want just look for stuff in strings. This will give you the FIRST occurrence of the match based on the expression in the string, and the length of the matched string.
Code: Select all
vString = 'TM1 is relatively ok!';
vExpression = 'el';
Code: Select all
"TM1 is relatively ok!","el","9","2"
Code: Select all
vString = 'TM1 is relatively ok!';
vExpression = '1*!';
Code: Select all
"TM1 is relatively ok!","1*!","3","19"
Known limitations/features:
- The parser is case-insensitive
- The parser runs maximum of 256 runs over the same string. This is deemed sufficient. Increase this value at your own risk
- When adding to the matched string, you can enter text in front of the $& or behind it. Or both.
- While not necessary, you should feed logic instructions to the parser. Feeding c*?s works as does c*s to find Cognos
- There is no way to escape characters used for the parser. This means that you are not able to find strings matching asterisk or question mark signs.
- No support for 'true' regular expressions such as ^[a-zA-Z0-9_.+-]
You run the Regex_test process for testing.
The logic is in the Sys_regex_replace and Sys_regex_find processes.