Extract a number from a summary based on substring


Please help me out as I am new to UIPath.
I have a Summary String.

Summary =
"Total Product(s) in batch
Variants - 51 Packs - 49

Successfully Uploaded
Variants - 45 Packs - 44

Upload Error
Variants - 11 Packs - 10

Under QC Approval
Variants - 6 Packs - 5

QC Rejected
Variants - 20 Packs - 19

Article Pending Creation
Variants - 16 Packs - 15

Article Created
Variants - 46 Packs - 45"

I need the Pack number under a heading (substring). For example:
Total Product(s) in batch 49
Successfully Uploaded 44
Article Created 45
Under QC Approval 5

Please share a regex code or any other way of getting this.

Thanks in advance.

@Rounak_Kumar - I would create 4 separate variables, one for each of the values you want. I will call these TotalProducts, SuccessfulUpload, ArticleCreated, and QCApproval. Each one of these variables will be found individually using string manipulation on the original the Summary string.

I have based these regex on your sampe text - mostly the only thing to change is the word(s) to look for in the positive lookahead. Below are the expressions:

TotalProducts = (?<=Total Product\(s\) in batch\r\n.+)\d+(?=\r|\n|$)
SuccessfulUpload = (?<=Successfully Uploaded\r\n.+)\d+(?=\r|\n|$)
ArticleCreated = (?<=Article Created\r\n.+)\d+(?=\r|\n|$)
QCApproval = (?<=Under QC Approval\r\n.+)\d+(?=\r|\n|$)

How this regex works: It looks for the keyword in the preceding line (newline is indicated by \r\n) and ignores all of the information in the next line until it gets to digits that is immediately before the next newline or end of input


Thank you so much.

Hi Dave,

I tried this solution in text file. It is working fine. But when I tried to do the same thing in excel (pasted this text in one cell of excel) and tried to extract using same method, UIPath is throwing Error:
System.NullReferenceException: Object reference not set to an instance of an object.

I tried to print the text but it look the same as the text in Notepad.

Am I missing something? Please help.

How are you converting the information from excel into a string variable to run the regex?

If you are using read range that will give you a datatable in return. You have to find the specific row and column in your datatable to convert it to string. For example: dt1.rows(0).item(0).tostring would turn the first row and first column into a string.

If you use read cell, it should just go straight into a string variable if you choose to do so in the output property of the activity

Just an fyi - NullReferenceException is a common error that occurs. It just means you are trying to use a variable in your code that was never initialized, so it is null

Main.xaml (7.4 KB) Sample Data.xlsx (9.0 KB)

Hi Dave,
I am using Read Cell activity.
I have attached my UIPath file and Sample excel file that I used.
Please try it once and let me know where I am doing wrong.

Try updating the regex as follows: (?<=Total Product\(s\) in batch(\r|\n|\r\n).+)\d+(?=\r|\n|$)

1 Like

Yes, this worked.
Thanks for the help.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.