cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous22
New Member

Inserting delimiter in string

Hello,

 

I have a situation where I am pulling a string value from a document processing power app. When I get the string in power automate it outputs as a block of sentences see example below:

 

"This is the first sentence.This is the second sentence.This is the third."

 

I need to be able to put a delimiter into the string after every "." If it is followed by an uppercase letter. The reason for this is that the sentences are  put into an array later in the flow.

 

My issue is sometimes there is "." Or capital letters in the sentence so I need to be very specific and only insert a delimiter if the period is followed by an upper case letter as this always indicates the end of a scentence in the string.

 

Is there any way to achieve this with an expression? Any help is greatly appreciated!

 

 

3 REPLIES 3
v-chengfen-msft
Microsoft
Microsoft

Hi @Anonymous22 ,

Do you want:

1.Split blocks of sentences with periods.

  1. If the first letter of the sentence is capitalized, add a delimiter to the sentence.

Here is a test for you reference:

I enter text by [Manually trigger a flow]

Note: [<br>is a delimiter.]

vchengfenmsft_0-1666592364939.png

vchengfenmsft_1-1666592364947.png

 

Here is result:

vchengfenmsft_2-1666592364950.png

 

Best Regards

Cheng Feng

Hi @v-chengfen-msft,

Thank you for that. One problem I am facing is some of the sentences contain thing like "e.g." or "ect.)."

In the string seeing as there is no spaces between the sentences is there a way to adjust the above to only insert the delimiter where the end of the sentence is followed by a capital letter?

Ex: "This is the first sentence ect.).This is the second sentence."

grantjenkins
Community Champion
Community Champion

Whenever I see something like this I think of Regular Expressions (RegEx).

 

We don't have anything natively available to use in Power Automate with regards to RegEx, but we can leverage Excel Office Scripts which is a really powerful way to extend what we can do in Power Automate (without having to purchase third-party solutions, etc.).

 

I generally have an Excel file stored in a location accessible to everyone in the company (read-only) where I add useful scripts that people can use. For this example, I created one to substitute text using the Replace method.

 

I've got a Library with an Excel file called Scripts.xlsx and have created an Office Script called regexReplace.

 

grantjenkins_0-1666616190705.png

grantjenkins_1-1666616335006.png

 

To create a script, click on Automate > New Script.

 

The code for the regexReplace script is below:

function main(
  workbook: ExcelScript.Workbook,
  searchString: string,
  regexPattern: string,
  regexFlags: string,
  replaceString?: string): string {
  if (typeof (replaceString) === 'undefined') {
    replaceString = '';
  }

  let re = new RegExp(regexPattern, regexFlags)

  return searchString.replace(re, replaceString)
}

 

You can then use this script in your Power Automate by using the Excel Online (Business) action Run script.

 

The full flow is below. I'll go into each of the actions.

grantjenkins_6-1666617398486.png

 

Initialize variable just adds your sample text for the example into a string variable called input.

grantjenkins_7-1666617426995.png

 

In this example I'm using the following text:

This is the first sentence ect.).This is the second sentence.And the third.

 

The Run script Substitute is the Excel Online (Business) action called Run script. This calls the Office Script passing in the relevant parameters including:

  • Script - the script within your Excel file.
  • searchString - the string you want to search.
  • regexPattern - the pattern you want to match on.
  • regexFlags - what regex flags you want to use.
  • replaceString - what you want to replace the matches with.

In our case we pass in the variable input for the searchString.

 

For the regexPattern we use ([.])([A-Z]) which has two groups. The first one will match on a single period, and the second one will match on a single uppercase character. So, it would match on .T or .H for example. We have the groupings () so that we can extract out the actual values found and substitute them back in with addition characters we want. In my example I insert two ++ between the . and the uppercase character so I can split on ++ later. You can add whatever you want, but I went with ++ since I'm sure we wouldn't see this in any of your sentences.

 

I've just used g for the flags, so it doesn't stop on the first match.

 

And finally, we build up the result which is the value returned from the first group - a single period, the characters ++, then the value returned from the second group - the uppercase character. .++T or .++H for example.

 

([.])([A-Z])

grantjenkins_5-1666617236271.png

 

The output for this example would be:

"This is the first sentence ect.).++This is the second sentence.++And the third."

 

Lastly, I just use a Compose to split the result on ++ so we get each sentence as a new item in our array.

split(outputs('Run_script_Substitute')?['body/result'], '++')

grantjenkins_8-1666617505100.png

 

Outputs from the flow run below:

grantjenkins_9-1666617624788.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.

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.

Community Calls Conversations

Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Users online (1,662)