How to parse a string

I have to read an email body and get needed variables. It looks like this:

“Project”:“Testing”
“Type”:“Problem”
“Description”:“A long description”

I wrote sth like this in Assign activity:

EmaiBody.Substring(EmailBody.IndexOf(strings.Chr(34)+“Project”+strings.Chr(34)+":")).Split(Environment.NewLine.ToCharArray)(0)
but it doesnt work :frowning:

Thats for the first one. I have to perform such action for each line. So for example Project string should store Testing string.

Thanks for help,
Logan

Could you also provide the expected output and preferably more examples?

So you want each line of the string to be assigned to the field name correct?
I am assuming the email body template it consistent correct?

Yes, it is second example. Where same variables should store other values:
“Project”:“Developement”
“Type”:“Issue”
“Description”:“No Description”
Yes it is :slight_smile:

“Project”:“Developement”
“Type”:“Issue”
“Description”:“No Description”

so Project string should store Developement or Testing Value. Type string - Problem or Issue value.

Are those regular double quotation marks? (It’s hard to tell since UiPath Form changes them automatically.)

image

If they are regular quotation mark you can extract the text with the following regular expressions:

(?<=\u0022Project\u0022:\u0022).+(?=\u0022)
(?<=\u0022Type\u0022:\u0022).+(?=\u0022)
(?<=\u0022Description\u0022:\u0022).+(?=\u0022)

E.g.

varProject = System.Text.RegularExpressions.Regex.Match(EmailBody, "(?<=\u0022Project\u0022:\u0022).+(?=\u0022)").ToString
varType = System.Text.RegularExpressions.Regex.Match(EmailBody, "(?<=\u0022Type\u0022:\u0022).+(?=\u0022)").ToString
varDescription = System.Text.RegularExpressions.Regex.Match(EmailBody, "(?<=\u0022Description\u0022:\u0022).+(?=\u0022)").ToString

image

image

it looks like this. I erased some personal data.

Yes, they are regular quotations marks so the regular expressions provided should work fine.

I get : Assign: Value cannot be null.
Parameter name: input

But body looks like this. It has @ at the beggninig:

@""“Project”":"“Test”"

I think this Regex is not correct. I have no matches here :frowning:
image

Hi @logan.capp,

Here is an alternative way to get the values by converting your string to json format.

You will need to install UiPath Web activities to use the Deserialize Json

The content in jsontext.json is your string. But mind the two different kinds of double quotes. json requires " and not the other slanted double quotes.
jsontext.json (73 Bytes) Json.xaml (6.7 KB)

It doesn’t work for you because the input text in the regex tester has a different kind of quotation marks. Probably the text was copied from UiPath Forum. The regular quotation marks are vertical while the other ones are leaning to the right.

image

Also the sample you provided in your first post doesn’t match the one you provided below. It has several quotation marks around Project and Test.

image

If you save the EmailBody variable to a text file (Write Text File activity) and upload it here, we can help you create better regular expressions. (You could of course replace sensitive data with other text. Just keep the formatting.)

1 Like

Yes, You are right again :slight_smile: Thanks, but still I have some difficulties.

Again, I get this in UiPath as the Email.body

@""“Project”:"“Testing”"

““IssueType””:"“Problem”"

““Summary””:"“A long summary”"

I havent realized earlier it is formatted like this. So many quotations… Should I add more u0022 ?

Please provide a text file or use the Preformatted text option.

image

You should have gotten a match even for the new input if those are regular quotations. But since you didn’t get any match at all, I suspect that the input text might contain different kinds of quotation marks.

image

1 Like

That is really weird, because in the website it works, but in a uipath doesnt.

varType = System.Text.RegularExpressions.Regex.Match(EmailBody, “(?<=\u0022Type\u0022:\u0022).+(?=\u0022)”).ToString

Even isMatch gives me False.

Hi @logan.capp

To use regex expression use matches activity

Thanks
Ashwin.S

Test with this more forgiving pattern. Basically it ignores any surrounding characters.

(?<=.{1,3}Project.{1,3}:.{1,3})[^\u0022\u201C\u201D]+

image

So, You are pro and Im really thankful. With the System.Text.RegularExpressions.Regex.Match in assign activity I get errors but If I use Matches activity and then assign to a result of match it works… I dont know why. Thank You one more time.

1 Like

have one more question if You still here.

I can have such cases:

““Labels””:["“Mailbox”", “Office”]

With Your Regex I get only Mailbox. Is it possible to get all the variables herefrom to a array?

You input text starts to look more and more like JSON. If it is, then @jeevith’s solution is probably better.

If you want to continue with regex, you need to do it with two patterns. First extract the all the text after “Labels”, then split it into an array and remove empty entries.

varLabels = System.Text.RegularExpressions.Regex.Match(EmailBody, "(?<=.{1,3}Labels.{1,3}:.{1,3})\w+[^\]\r\n]+(?=\]?)").ToString
arrayLabels = System.Text.RegularExpressions.Regex.Split(varLabels, "[\u0022\u201C\u201D, ]+").Where(Function(x) Not String.IsNullOrEmpty(x)).ToArray()

image
image