Regular expressions (regex) are powerful tools that help you search for and manipulate text patterns in your fields. Functions like regexmatch
, regexreplace
, and regexescape
make it easy to perform complex text operations without needing extensive programming knowledge. This article will help you understand how to use these functions effectively.
For more on Patterns and Regex please click here.
regexmatch([Field], 'Pattern')
The regexmatch
function checks if a certain pattern (a sequence of characters) is found in a field. This pattern is written in a special format called a regular expression (regex). If the pattern is found in the text, the function returns true; otherwise, it returns false.
Note: \d is a digit (a character in the range [0-9]), and + means one or more times. Thus, \d+ means match one or more digits.
Note: In order to end up with a regex pattern containing \d (to match any digit) you need to write it as \\d in NCalc
Example: If you have a field called Phone Number [PhoneNumber] and want to check if it contains any numbers, you can use: regexmatch([PhoneNumber], '\\d+') This will return true if [PhoneNumber] contains any numeric character, otherwise false.
[PhoneNumber] = '07765456543'
[Outcome] = true
regexreplace([Field], 'Pattern', replacement)
The regexreplace
function searches for a pattern in a field and replaces all occurrences of this pattern with a specified replacement string.
Example: If you want to replace every number in a client costing proposal, in a field called Proposal [Proposal] with an asterisk (*), you can use: regexreplace([Proposal], '\\d', '*'). This will return the text in [Proposal] with every numeric character replaced by *.
[Proposal] = 'Odyssey starts on a budget of £30000 with Max at the helm, the intent is to transform the user experience from start to finish'
[Outcome] = 'Odyssey starts on a budget of £***** with Max at the helm, the intent is to transform the user experience from start to finish'
regexescape([Field])
The regexescape
function is used to make sure that special characters in a text are treated as normal characters. This is useful when you want to search for a text that includes characters that have a special meaning in regular expressions, such as .
or *
.
Note: When using a pattern directly in NCalc, you need to escape special characters like '
and \
by adding another \
before them.
Example: If you want to check if a project description [Project] contains your name, you can use: regexmatch([Project], regexescape('Max')). This will return true if [Project] contains the literal string 'Max', otherwise false.
[Project] = 'Odyssey starts on the 12th with Max at the helm, the intent is to transform the user experience from start to finish'
[Outcome] = true
Example: If you want to check if a project description [Project] contains a codename, you can use: regexreplace([Project], regexescape('Odyssey'), '******'). This will remove any references to 'Odyssey' for ******.
[Project] = 'Odyssey starts on the 12th with the intent to transform the user experience from start to finish'
[Outcome] = '****** starts on the 12th with Max at the helm, the intent is to transform the user experience from start to finish'
Comments
0 comments
Please sign in to leave a comment.