cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ops_geo
Helper I
Helper I

Get the middle sentence between words from email subject line

Hi I have created a simple flow where I extract the middle sentence from an email subject line. The subject line is usually in this format:

Attention - Department name, random words

 

I was able to extract the middle sentence using this formula in my initialize variable function:

first(split(last(split(outputs('Compose'),'- ')),',')) 
 

However, now I am finding that people are adding all sorts of "-" that is messing up the flow, for example, we have received emails with subject line that look like this:

1. Attention - Department Name, random words - more random words

2. Attention - Department Name - random words and more random words

 

I was wondering, is there a formular that will always extract words in between the first "-" and "," and ignore any subsequent "-" or ","

In some instances, people may put the Department Name in between two "-" "-" (see example 2 above).

How can I add that to my formula so that I can get the Department Name only?

If someone could please help me with that, I would much appreciate it. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
grantjenkins
Community Champion
Community Champion

This is how I'd probably extract out the department. See expression below:

 

trim(first(split(split(outputs('Compose'), '-')[1], ',')))

 

grantjenkins_0-1669953197566.png

 



----------------------------------------------------------------------

If I've answered your question, please mark the post as Solved.

If you like my response, please consider giving it a Thumbs Up.

View solution in original post

6 REPLIES 6
srApps
Resolver III
Resolver III

One way that worked in both of your examples was to do something like this where you can do it in two stages: first handle the "-" then handle the "," after.

srApps_0-1669919296268.png

Compose 2 is just this:

split(outputs('Compose'), '-')
This leaves you with this:
srApps_1-1669919353916.png

Compose 3 then checks to see how many times it split based on how many "-" there were in the string:

if(greater(length(outputs('Compose_2')), 2), trim(outputs('Compose_2')[1]), trim(last(outputs('Compose_2'))))
 
If it splits it into > 2 strings, get the 2nd with the index of [1], else get the last portion after the only "-".
 
Then, handle the "," in Compose 4:
first(split(outputs('Compose_3'), ','))
 
Should work as long as the department always comes after the first "-" and someone doesn't put another one before it.
grantjenkins
Community Champion
Community Champion

This is how I'd probably extract out the department. See expression below:

 

trim(first(split(split(outputs('Compose'), '-')[1], ',')))

 

grantjenkins_0-1669953197566.png

 



----------------------------------------------------------------------

If I've answered your question, please mark the post as Solved.

If you like my response, please consider giving it a Thumbs Up.
srApps
Resolver III
Resolver III

Concise, I like it. A simpler solution

@grantjenkins 

You area resident rockstar indeed!
quick question, what was the reason for removing "last" from the formula? Also, does [1] in bracket specifies the position of the item I want?

If we work from the inside out.

trim(first(split(split(outputs('Compose'), '-')[1], ',')))

 

We use split to try and split on '-'. This will result in an array where we want the second item which is index 1 or [1]. Note that the first item at index 0 would be 'Attention ' in this instance.

 

We then use split to try and split on ',' and take the first item this time. We could have also just used [0] to get the first item.

 

Lastly, we use trim to ensure there are no preceding or trailing spaces around the Department name.



----------------------------------------------------------------------

If I've answered your question, please mark the post as Solved.

If you like my response, please consider giving it a Thumbs Up.

@grantjenkins 

makes sense, thank you so much for the explanation! I was wondering is the code only meant to work for "-" because I have noticed in emails that some - gets autoformatted to – (the longer version of the - which is so annoying because it breaks the flow, so I was wondering is there a way to consider both versions of the - and – ?

If you could please let me know, that would be great!

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Users online (5,091)