Skip to main content
Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Unanswered

Extracting first instance in string - Expression

(0) ShareShare
ReportReport
Posted on by

Hello - fairly new to Microsoft Flow and loving it.

 

Looking to see if there is a way to get the first instance of a text from the body of an email.  I have Initialized a variable called EmailBody with the type String, and the value as the body of the microsoft email.

 

So the HTML of the email may look like

"<tr><td class="fieldTitle"><label>Product:</label></td><td class="fieldData"><label>Carrots</label></td></tr>"

"<tr><td class="fieldTitle"><label>Product:</label></td><td class="fieldData"><label>Apples</label></td></tr>"

 

I want to get the first instance of Product: and store the variable Carrots

 

 

To do this what I have been doing is splitting the EmailBody variable, and used the last part of it.  I guess the hard part is that it splits the last instance - in this case the last instance of "Product: ".  But what do I use for the expression if I want to get the first instance of "Product: "

 

Initialize Variable

Name: ProductValueStep1

Type: String

Value (expression) =   last(split(variables('EmailBody'),'Product: '))

 

Initialize Variable

Name: ProductValue

Type: String

Value (expression) =   first(split(variables('ProductValueStep1'),'</label>'))

  • Ed Gonzales Profile Picture
    4,531 Most Valuable Professional on at
    Re: Extracting first instance in string - Expression

    @scottareid 

    So, an interesting way to go about this...but it's a fun approach.  Here's something that might help:

    When you use split(), it takes a string and creates an array based on the delimiter you choose.

    You can select specific elements of an array by adding the element number at the end (keep in mind it starts at zero).  So if you had the text:

    "<tr><td class="fieldTitle"><label>Product:</label></td><td class="fieldData"><label>Carrots</label></td></tr>"

    And you use the expression:

     split(variables('EmailBody'),'Product: ') then you will end up with two elements, everything before the delimiter ("Product: "), and everything after:

    Element 0 would be: <tr><td class="fieldTitle"><label>

    Element 1 would be: </label></td><td class="fieldData"><label>Carrots</label></td></tr>

    Maybe try splitting on the '<label>' and then taking the3rd element:  split(variables('EmailBody'),'<label>') would give these results:

     

    Element 0: <tr><td class="fieldTitle">

    Element 1: Product:</label></td><td class="fieldData">

    Element 2: Carrots</label></td></tr>

     

    So, split(variables('EmailBody'),'<label>')[2] would give you Carrots</label></td></tr>

     

    Then, split that again on '</label>' and take the first part:   split(split(variables('EmailBody'),'<label>')[2],'</label>')[0] should give you just "Carrots" (No quotes).  Here's a video on split:

     

     

    Keep us posted.

    -Ed

     

    If you liked this reply, please give it a thumbs up! If this reply has answered your question or resolved your challenge, please consider marking it as a Solution. This helps other users find it more easily via search.

     

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 497 Super User 2025 Season 1

#2
David_MA Profile Picture

David_MA 436 Super User 2025 Season 1

#3
Riyaz_riz11 Profile Picture

Riyaz_riz11 244 Super User 2025 Season 1