How to extract last paragraph from a Text

I need to extract the last paragraph from the below text i.e the paragraph starting from Reply. Please note there is no common seperator that I can use. The last paragraph can start with different words like Reply or Internal Note or Remark.
I tried using the Split("\n) method but it did not work.

Internal Note 12345678 06.11.2023 18:04:24
11/6 - Attached signed DR and BOL, signed BOL shows only 6 pallets were
picked up. Requested for cycle count. AAN

Internal Note 3456789 22.11.2023 21:13:13
11/22 - Attached POD documents show that material was picked up by
FedEx. 7 pallets were consolidated into 6 physically. Total weight of
material shipped when picked up equals the weight of material received.
DR also shows all 514 cases received by BBY. Deduction is invalid. To be
collected in full. AAN

Internal Note 23456789 22.11.2023 21:48:13
PRD to be issued.

Reply 123456789 22.11.2023 21:57:07
Please be advised that your deduction of $20,481.48 referencing
DMQ891XXXXXX is denied. The attached shipping documents demonstrate all
the material as having been shipped and delivered (weight of the load
shipped/invoiced is equal to the weight of the load received) 7 pallets
were consolidated physically into 6 pallets, hence the change in # of
pallets. Please remit payment immediately, referencing DMQXXXXXXXXXX on
your remittance advice. Thank you. AAN.

2 Likes

Hi @Rajesh_Yadav

You can check the below expression to get the last paragraph in your input, store the Input data in a variable called Input.

- Assign -> Output = Input.Split(System.Text.RegularExpressions.Regex.Match(Input.ToString,"\n\s").Value).Last

Output variable contains the last paragraph.

Check the below workflow for better understanding,

Hope it helps!!

Hi,

Follow whatever @mkankatala suggested in above post. If you faced any issue, then refer below xaml file.

extract last paragraph forum.zip (3.0 KB)

Thanks

Thanks for your quick solution. The solution you mentioned works if I pass the entire text in an assign activity as a value to input variable. But when I directly extract this entire text from SAP using Get Text activity and the output of that activity is stored in a variable called Inputtext. Then I use another Assign activity where Input variable is LastParagraph = InputText.Split(System.Text.RegularExpressions.Regex.Match(InputText.ToString,“\n\s”).Value).Last

When i do this the value in LastParagraph is the entire text again.

The Output of GetText activity is below image…

and the above image is also the value of LastParagraph

If you extract the text from SAP by using Get Text, the output of Get Text is also String. Then you can use the same expression to get the last Paragraph in the Extracted data… @Rajesh_Yadav

What is the LastPragraph Variable is printing now for you. Are you getting the required output…?

I am getting the same output that I got with GetText activity. And this is the value I have used to store in LastParagraph.

Hi,

Can you try the following expression?

System.Text.RegularExpressions.Regex.Match(strData.Trim,"(?<=\r?\n\r?\n)[\s\S]+?$",System.Text.RegularExpressions.RegexOptions.RightToLeft).Value

Sample
Sample20240320-1a.zip (3.1 KB)

Regards,

Hi Yoichi,

I tried your solution but I am getting a blank result. Please find below image.

InputText is the output of the GetText activity.

Hi,

If possible, can you share your string as text file using WriteTextFile activity?

Regards,

I just did that. Extracted the data from SAP using GetText activity and then stored that string in a Text file using Write Text File activity. Then used a Read Text file activity to read it back again and then applied Regex but still the same.

Can you share the file here, if possible?

Regards,

Test.xaml (9.9 KB)

Hi,

It seems workflow file. Can you share the text file from GetText (data2.txt?)?

Regards,

Split(yourStringVar,VbCrLf).Last

There is no reason to do this. You already have it in a variable after using Get Text.

data2.txt (1.2 KB)

I just noticed the string extracted from SAP is getting stored as a single paragraph into Data2.txt

All right

As Linebreak is just CR, the following expression will work. Can you try this?

System.Text.RegularExpressions.Regex.Match(strData.Trim,"(?<=\r\r)[\s\S]+?$",System.Text.RegularExpressions.RegexOptions.RightToLeft).Value

Sample
Sample20240320-1a (2).zip (3.8 KB)

Regards,

Thanks for being so prompt with this, I will check this and get back to you soon.

Hi Yoichi,

It works now. Thanks a Lot !!!

Just one more question how do I get all other paragraphs except the last one in a different variable?

Hi,

If you also need other paragraph, the following expression is better.

arrParagraph = System.Text.RegularExpressions.Regex.Split(strData,"\r\r+")

Sample
Sample20240320-1aV2.zip (4.0 KB)

Regards,