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:
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!
Solved! Go to Solution.
This is how I'd probably extract out the department. See expression below:
trim(first(split(split(outputs('Compose'), '-')[1], ',')))
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.
Compose 2 is just this:
Compose 3 then checks to see how many times it split based on how many "-" there were in the string:
This is how I'd probably extract out the department. See expression below:
trim(first(split(split(outputs('Compose'), '-')[1], ',')))
Concise, I like it. A simpler solution
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.
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!