Regex grab value for identifier

Hello

I am looking for a regex expression that will extract the desired value for a specific key, under a specific identifier.
The keys could move.

Example:

  • Identifier: PersonDetails
  • ValueForKey: lastname

Should give me the result “Doe”.

"elements":[
{
"identifier": "JobDetails",
"id": "asda892-241sad2-214sfs",
"values": {
"work address": "Random Road 2",
"city": "Random City",
"firstname": "Bob",
"lastname": "Johnsen"
}
}
{
"identifier": "PersonDetails",
"id": "sd89asd-gasg89-gasdf23",
"values": {
"firstname": "John",
"lastname": "Doe",
"age": 39
}
}
]

Regards
Soren

Hi @SorenB

(?<="identifier":\s+")\w+

(?<="lastname":\s+")\w+

Regards,

@SorenB

System.Text.RegularExpressions.Regex.Matches(Input,"((?<=""identifier"":\s+"")\w+)").Last.ToString
System.Text.RegularExpressions.Regex.Matches(Input,"((?<=""lastname"":\s+"")\w+)").Last.ToString

What if the same key could be for more that one identifier.
I have updated the example.

I want the regex to find my desired “identifier” and then zoom to the “values” to search for the desired key inside those.

String looks like JSON, so we can process with JSON approaches
The image shows a JSON string containing job and person details, and a C# LINQ query that retrieves the lastname "Doe" from the person details. (Captioned by AI)

1 Like

we suggested above to base line on JSON. However a regex pattern could like this


and can also be finetuned

Hi @SorenB

Please find the below solution

  1. Use assign activity to find the identifier = PersonalDetails for rest of other details
strLastName = System.Text.RegularExpressions.Regex.Match(strInput, """identifier"":\s*""PersonDetails""[^{]*\{[^}]*""lastname"":\s*""([^""]+)").Value.ToString.Trim
  1. User assign acitivity to get the last name using the regex
strLastName = System.Text.RegularExpressions.Regex.Match(strLastName, "(?<=""lastname"":\s)(.*)").Value.ToString.Replace("""", "")

Output:
image

Thanks,
Purushotham

1 Like

Hi @batthula.p949 @SorenB

The strInput variable are the input variables that has my JSON ?

Hello @ppr

Great thanks that seems like a usable approach :grin:

Best regards
Soren

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