Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Application specific solutions
    • Dataverse
    • Excel
    • Forms
    • Outlook
    • Planner
    • Power Apps
    • SharePoint
    • Teams
  • Triggers
  • Ready to use solutions
    • Approval Process Solution
    • Task Delegation App
    • The Ultimate Power Automate expressions cheat sheet
    • Power Automate HTTP requests to SharePoint cheat sheet
    • Power Automate HTTP requests to Graph API cheat sheet
  • ABOUT ME
  • Get help with your flow
  • POWER PLATFORM SERVICES
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.


🚀 Master Power Automate

Join 2,100+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

14 thoughts on “Three expressions to extract a piece of text in Power Automate”

  1. Selom says:
    September 7, 2023 at 4:50 pm

    If I wanted to extract the first character of each name, how’d I go about that?

    I tried with no joy:
    substring(variables(‘StudentFirstMidName’), add(indexOf(variables(‘StudentFirstMidName’), ‘ ‘), 1), 1)

    So for example, Montague Middles Names Miller, I would want MMNM. My first and middle name is one MS Form entry and last name is another (no issues extracting that with substring(variables(‘StudentFirstMidName’), 0, 1)

    Any insight is greatly appreciated!

    Reply
    1. Petar says:
      September 27, 2023 at 4:01 am

      While I’m not an expert, far from it, I would recommend using split function by the ‘ ‘ character. That would create a list with 3 strings, first, middle and last name.
      Then you can just do a substring on each for the first character and combine them with concat fucntion.
      It might looks something like this:
      concat(
      substring(split(variables(‘StudentFirstMidName’), ‘ ‘)[0], 0 ,1), substring(split(variables(‘StudentFirstMidName’), ‘ ‘)[1], 0 ,1), substring(split(variables(‘StudentFirstMidName’), ‘ ‘)[2], 0 ,1)
      )

      Reply
      1. Selom says:
        February 9, 2024 at 3:36 pm

        Hi Petar, thank you for getting back and apologies for my late response, I didn’t get a notification email or my poor personal inbox management got me.

        In the end I did this:
        Select
        From: split(trim(variables(‘StudentFirstMidName’)),’ ‘)
        Map mode: substring(item(),0,1)

        and this achieved what I wanted. I placed the output for Select and another compose I did for the last name next to each other.

        Reply
    2. Tom says:
      September 30, 2023 at 10:19 pm

      Hello Selom,
      you can split it by ‘ ‘, take() 1 character from each name, and join them back, e.g.
      concat(take(split(‘Tom Riha’, ‘ ‘)[0],1), take(split(‘Tom Riha’, ‘ ‘)[1],1))

      Reply
      1. Selom says:
        February 9, 2024 at 3:36 pm

        Hi Tom, Apologies for my late response.
        In the end I did this:
        Select
        From: split(trim(variables(‘StudentFirstMidName’)),’ ‘)
        Map mode: substring(item(),0,1)

        and this achieved what I wanted. I placed the output for Select and another compose I did for the last name next to each other.

        Reply
        1. Selom says:
          February 9, 2024 at 3:38 pm

          ah sorry I did some more compose for the Select with a Join.

          Reply
  2. Elizabeth G says:
    September 24, 2024 at 9:41 pm

    How can I get this to work if the “incident number” is sometimes 4 or sometimes 5 characters? It doesn’t seem like the expressions care about the count of characters within the square brackets but I can’t seem to figure it out 😉

    For my purposes I am looking for text ‘Service Provider: ‘ where it could be either ‘Service Provider: abcdef’ or ‘Service Provider: xyz’

    Thx

    Reply
  3. karim says:
    October 23, 2024 at 7:04 pm

    Hi,
    Thank you for this great post. It is really good detail.
    My question is about trying to extract two values from the subject line of an email.

    [app name] [ app status]

    How to I get both of those values from within the brackets. Thank you!

    Reply
  4. Johnson Ramotsisi says:
    November 1, 2024 at 11:21 am

    How can i get a certain portion of the email subject?but the subject is always changing

    Reply
  5. Roshan says:
    December 30, 2024 at 11:51 am

    when i am extracting afrom .txt document, the slice is not reading line breaks , so the output is single continous paragraph.

    Reply
  6. Aaron says:
    March 19, 2025 at 10:54 am

    How are these going to work when there are repeating patterns in a string? Knowing the exact ending index positions is impossible.

    “teams(‘teamId’)/channels(‘channelId’)/messages(‘messageId’)/replies(‘replyId’)”

    Reply
    1. Tom says:
      April 5, 2025 at 9:00 am

      Hello Aaron,
      it won’t. If there’s nothing unique in the string you can catch on then these expressions won’t help you.

      Reply
  7. Toni says:
    April 7, 2025 at 1:40 pm

    Hello,
    what do I do, if I know the string length but just the beginning of the string is constant but the rest of it is dynamic in every email?
    Thank you for your help.

    So long,
    Toni

    Reply
    1. Tom says:
      April 19, 2025 at 11:44 am

      Hello Toni,
      you always need something to catch on, some word, position, character, something that’ll help you identify the right place.

      Reply

Leave a Reply Cancel reply

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

🚀 Master Power Automate

Join 2,100+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

Still exchanging emails to get things approved?

Get the Approval Process solution to skip the hard part and deploy an automated, fully functional approval solution on a SharePoint list in minutes! And then the next one, and the next one...

Approval Template Preview ✨ Learn more ✨

Are your approvals stuck due to unavailable approvers?

Keep your approvals running with the Approval task delegation app! Reassign any existing approval task to another user with a single click - no more waiting for absent approvers!

Power Automate approval task delegation ✨ Learn more ✨

Turn ideas into flows with ease!

Grab the complete Power Automate Cheat Sheet Bundle—everything you need to master expressions, SharePoint HTTP calls, and Graph API in Power Automate.

Cheat Sheet Bundle Preview ✨ Get the Cheat Sheets

Didn't find what you were looking for?
Need to adjust a solution to fit your needs?
Or would you just like to get an assistance from somebody with thousands of hours of experience with Power Automate?

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

©2025 Let's POWER Automate | Theme by SuperbThemes