How to extract a number by length from the given input string?
Use Case
String parsing is a common scenario when building workflows to solve business problems. In this article, I have taken a complex problem of extracting number from a given string. I will demonstrate how we can create a utility flow by using expressions, select & filter actions to perform the extraction operation.
Scenario
An input string that contains order number needs to be separated & extracted from a given string. Following is one of the example of an input string:
Demo to extract 5 digit order#12345 from the given input string
Solution
We will create a cloud flow to perform extraction of a number from a given input string.
1. Firstly, lets create a flow and add “Manually trigger a flow” as the trigger.
Add 2 parameters: Input String (String) & Digits Length (Number)
2. Next, add “Compose” action to create an array of all characters in the input string parameter. We will apply expression to convert entire input string parameter to array of characters.
Below expression needs to be added in the ‘Function’ box as highlighted in the above screenshot:
chunk(triggerBody()?['text'],1)
3. Post creating array, add “Select” action to check each character of input string if it is an integer or not using expression. If its an integer then it retains its value else put ‘n’ for non-numeric characters. In ‘From’ parameter, we will generate list of numbers from 0 to length of compose action to create an array of integers that will be used for referring each character in input string
Expression used in ‘From’ parameter in the above screenshot:
range(0,length(outputs('Compose')))
Click ‘Switch to text’ button on right side of ‘Map’ parameter to switch to text mode:
Post switching Map to text mode, enter the below expression in the textbox:
if(isInt(outputs('Compose')[item()]),outputs('Compose')[item()],'n')
4. In the next step, add another “Compose” action. Using complex expression, we will join all the elements from the output of select action and recreate an array by splitting joined string by ‘n’ character (non-numeric). We will get an array with only numbers in the output after applying the complex expression:
split(join(body('Select'),''),'n')
5. Now, add “Filter array” action to filter out the required number from the input string. We will pass the output of previous action (Compose 1) in the ‘From’ parameter. We will apply expression in the ‘Filter Query’ parameter to validate the length of the number to be extracted:
Click ‘Edit in advanced mode’ button at the bottom of filter array action and enter the below expression in the ‘Filter Query’ parameter:
and(not(empty(item())),equals(length(item()),triggerBody()?['number']))
6. Before returning the extracted number, check if number exists or not in the given input string. To perform that check, add one more ‘Compose’ action. Enter an expression to check the output body of filter action, if output is empty then set value as empty string else set the extracted number in value of compose action:
if(empty(body('Filter_array')),'',first(body('Filter_array')))
7. Finally, add “Respond to a Power App or flow” action. Add an output parameter ‘Output’ of string data type and set the output of previous action (Compose 2) dynamically as its value:
Save the flow & ready to test it.
Output
Run & test the flow by entering the following values in input parameters.
Input String
Demo to extract 5 digit order#12345 from the given input string
Digits Length
5
Value of ‘Output’ parameter after flow execution:
Conclusion
- We learnt that we could use powerful actions: “Select” & “Filter array” to perform complex string parsing operations with the help of expressions.
- Using “Select” action approach is more efficient for long input as it avoids “Apply to each” (loop).