How to Extract Numbers from a String Using Power Automate

If you have a form or survey where users enter data (such as a phone number), you may want to validate that the data entered is correct. Power Automate doesn’t provide a built-in function to extract numbers from a string. In response to a question posted on the Power Automate community forum, I wrote the following custom flow that can parse through a string and extract any numerical values contained within it. While there are various different ways to tackle this issue, I think that the flow presented below is easy to understand and implement.

The Flow

The variable varData stores the string from which we will extract the numerical values.

This is the flow solution. Initialise variable, Select action and a compose action.

How it works: the SELECT action

SELECT

FROM: 

chunk(variables('varData'), 1)

MAP: 

if(isInt(item()), item(), null)

The chunk function in Power Automate can divide or split a string into chunks of equal length. The expression in line 5 above splits a string into chunks of length equal to 1 and returns the results as an array. The Select action will loop through each item of the array, starting from the first character (“1”) and progressing to the last character (“0”). The expression in the Map field (line 9) will act on the each item of the array.

How the Select action works. Shows result of the Select action

The expression inside the Map field works in text mode and uses the relatively new IsInt() function to determine whether a character is an integer. If a character is an integer it is added to the output of the Select action. If a character is not an integer an empty value (null) is added to the output of the Select action. The resulting output of the Select action is an array containing numbers and null values.

How it works: the Join action

Finally the Join action is used to join all the items of the array into a string while removing any null values:

COMPOSE:

join(body('Select'),'')


How the Join action works. Shows result of the join action

Runtime Output

Runtime output of Select action

All the characters have been removed and we are left with only numbers:

Runtime output of join action. The resulting string contains only numbers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Website Built with WordPress.com.

Up ↑

%d bloggers like this: