Issue with Uipath Regex

I have a String value like below,

Input String = “(1) Bene Name : Adam \r\n(1) Total payee share : ₹5000 \r\n(1) Bene Address line 1: James street \r\n(1) Bene Address line 2 : New jersey”

I need to get the name “Adam” using Regex.
if I use the below Regex pattern,
Bene Name\s*:\s*([^\r\n]+) . It gets me all the sections of the string that is after "Bene Name: "
But I need only “Adam”

If I intentionally make the pattern like below,
Bene Name\s*:\s*([^(]+)
It gets me the right value, I.e. “Adam \r\n” - Values after colon and before “(”

\r\n are carriage return and new line feed characters itseems. So, Regex isn’t working in this case. So How to get the exact value.

I need to get Bene Name and Total Payee Share value from the above string. String manipulation was lengthy and that’s why thought of using Regex.

Hello @besakkiappan46

Please use the regex Bene Name\s*:\s*(\w+).*? for Adam and

Total payee share\s*:\s*₹?(\d+)

Hi @besakkiappan46

For Bene Name:
(?<=Bene Name\s*:\s*).*(?=\s+\\r\\n.* Total payee share)

For Total payee share below both regular expressions will work:
(?<=Total payee share\s*:\s*).*(?=\s+\\r\\n.* Bene Address line 1)

OR

(?<=Total payee share\s*:\s*)\₹\d+

Hope it helps!!

Hi @besakkiappan46

(?<=Name\s+:\s+)(\w+)

(?<=share\s+:\s+)(₹\d+)

Regards,

Hie @besakkiappan46 here your Regex Pattern
Your Input


Your Regex and output

System.Text.RegularExpressions.Regex.Match( strName, “Bene Name\s*:\s*(\w+)”).Groups(1).Value
Cheers Happy Automation

If I get 2 names, Like “Adam Jones”. It gets only Adam. How can I tweak the regex to this? @pradeep931

If I get 2 names, Like “Adam Jones”. It gets only Adam. How can I tweak the regex to this?

@besakkiappan46

You can add \s*\w+

Bene Name\s*:\s*(\w+\s*\w+).*?

@besakkiappan46 here the code use this

System.Text.RegularExpressions.Regex.Match(strName, “Bene Name\s*:\s*([\w\s]+)”).Groups(1).Value.Trim()

cheers Happy Automation

Thanks @pradeep931 . It worked.
But for Total payee share\s*:\s*₹?(\d+) , I had to use $ symbol in the string value (i.e. $5000). Replacing ₹ with $ didn’t work. So, I want to exclude the $ and get only the amount (5000)

1 Like

@besakkiappan46

Use below expression
Total payee share\s*:\s*$\d+

Hi @besakkiappan46

Your original pattern did not work because in regex a backslash character needs to be escaped with another backslash.

So text like \r need to be treated in your regex pattern like this \\r. Now your original pattern updated looks like this:

Some alternative solutions for your review
Bene Name - preview the pattern here:
Use in an assign like to trim the spaces
System.Text.RegularExpressions.Regex.Match(yourStr, “(?<=Bene Name\s+:)[^\]+”).ToString.Trim

Payee Name - preview your pattern here:


Use in an assign like to trim the spaces
System.Text.RegularExpressions.Regex.Match(yourStr, “(?<=Total payee share\s+:)[^\]+”).ToString.Trim

Hopefully this helps,

Cheers

Steve

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