An interesting use case that came up recently was the need to extract numbers from a multi-line text and then compare it to a threshold to conditionally format the text (change the fill property of the text input control).
It seemed easy to start off with but that changed soon because there was no particular format in which the users would input the text. This meant that the numbers could be anywhere in the text and that the position of the numbers within the text could not be set to a predefined value. To add to the complexity, the numbers could either be whole numbers or have decimal places.
The main function that is used to solve for this is the Match function. With the Match function, patterns need to be defined to specify what to compare or match the text to. Now patterns can be defined in multiple ways - using ordinary characters such as "abc", regular expressions such as "\d+", and predefined patterns such as Digit.
For the purposes of this example, let's assume the threshold is 30. In other words, if there is a number in the text which is greater than 30, then the text input control needs to be filled in red color, else in gray color. The way to achieve this conditional fill of the text input control is to set its Fill property to the following expression:
If(Value(Match(TextInput.Text,(MultipleDigits & "\." & MultipleDigits)).FullMatch)>30 || Value(Match(TextInput.Text,MultipleDigits).FullMatch)>30, ColorFade(Red,0.5), ColorFade(Gray,0.5))
Let's break this down:
- The first Match compares the text with the following pattern: MultipleDigits & "\." & MultipleDigits. MultipleDigits is a predefined pattern to match one or more digits and "\." denotes the decimal. This will ensure finding numbers with any number of decimal places. FullMatch ensures that all of the text string that was matched is returned. Finally, the Value function converts the returned text to number.
- The second Match compares the text with the following pattern: MultipleDigits. This ensures finding whole numbers. FullMatch ensures that all of the text string that was matched is returned. Finally, the Value function converts the returned text to number.
- The numbers returned from both the expressions are checked if they are greater than 30. If either of the two numbers are greater than 30, then the fill color is set to red, if not, to gray. ColorFade is used to fade the colors red and gray by 50% to make them more soothing to the eyes.
Here's a working example:
Have fun! Get addicted!