Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Triggers
  • Application specific solutions
    • Dataverse
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
  • Get help with flow
Menu

Three expressions to extract a piece of text in Power Automate

Posted on August 17, 2022August 17, 2022 by Tom

“I receive an email with a ton of information that I’d like to proces automatically, how can I extract the relevant text using Power Automate?”


There’re many situations when you have more information than you need. It can be a long email subject, file name with details on the file, or a whole structured email, all of them containing one piece of text that’s important for you. How do you extract it? What possibilities does Power Automate offer?

Slice(…)

The first expression you could use is slice(…). Slice expects 3 parameters – the whole string, the start position, and the end position.

slice(<string>, <startPosition - included>, <endPosition - excluded>)

For example, if I have an email subject “Incident number [#1324]”, I can get the incident number with the expression below.

slice('Incident number [#1324]', 18, 22)

But you probably don’t know the exact location of the text, which is why this expression is often combined with indexOf(…). IndexOf(…) expects 2 parameters, the string and the character you’re looking for, and it’ll return their position. E.g. to find the position of the #.

indexOf('Incident number [#1324]', '#')   = 17

If you combine it in a single expression with the slice(…) while searching for the ] as the end character…

slice('Incident number [#1324]', indexOf('Incident number [#1324]', '#'), indexOf('Incident number [#1324]', ']'))

…the result will be #1234.

Since the character on the start position is included, you must skip it by adding 1 to the start index.

slice('Incident number [#1324]', add(indexOf('Incident number [#1324]', '#'),1), indexOf('Incident number [#1324]', ']'))

Substring(…)

Another expression you could use is substring(…). It’s similar to slice(…) with a difference in the 3rd parameter. While slice(…) expects the end position, substring(…) needs the number of characters to take.

substring(<string>, <startPosition - included>, <numberOfCharacters>)

To get the incident number from the example above, it’d look as below.

substring('Incident number [#1324]', 18, 4)

Substring(…) is a good expression if you know how long is the text you need. If it’s an ID that’s always 4 characters long, you can use substring(…) with a single indexOf(…).

substring('Incident number [#1324]', add(indexOf('Incident number [#1324]', '#'),1), 4)

But if the text length can vary you’re better off with slice(…) or the last expression – split(…).

Split(…)

Split(…) is my favourite expression when extracting a piece of string. With split(…) you’re not extracting text directly, but you split the string into smaller pieces which are easier to navigate. Split(…) has two parameters – the string a the character to split by.

split(<string>, <character>)

Following on the example, you can split the string by the # character.

split('Incident number [#1324]', '#')

The result will be an array as below, which you can then easily navigate using indexes.

[
  "Incident number [",
  "1324]"
]

The number is the second row, that’s index 1.

split('Incident number [#1324]', '#')[1]

Remove the closing ] with the replace expression and you’re done.

replace(split('Incident number [#1324]', '#')[1],']','')
Power Automate extract text

Summary

Power Automate gives you three ways to extract a piece of text from a longer string. If you’re looking for a text with fixed length, the best approach might be the substring(…) expression. But if the text length isn’t fixed, it’s much better to use slice(…) or split(…), with my preference being the latter.

If you’d like to learn more on the other expressions, you might check the cheat sheet I created.


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Do you know what to do, but not how to do it?

Get The Ultimate Power Automate expressions cheat sheet and translate your thoughts into flows with ease!


NEW! Master the HTTP requests to SharePoint with a new cheat sheet!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Use Power Automate to forward Outlook events upon registrationJanuary 29, 2023
  • Why the condition is false for the same numbers (Power Automate)January 25, 2023
  • How to forward event invitation to other calendar (Power Automate)January 22, 2023
  • Run ‘For selected item’ flow from non-default environment (Power Automate)January 18, 2023
  • Hide button in SharePoint list after Power Automate flow startedJanuary 15, 2023

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

©2023 Let's POWER Automate | Theme by SuperbThemes