How to user a regular expression to extract the part of the information from string which have repetitive patterns

Hi all,

I’ve email body as string with me
e.g.,

Application: Netflix
Account : XYZ
Operation: Remove
Attribute: Access Rights
Value: Write

Application: Netflix
Account : XYZ
Operation: Disable

I want only below mentioned part of the string

Application: Netflix
Account : XYZ
Operation: Disable

Below are the regex pattern to extract the value
(?<=Application: ).*
(?<=Account: ).*
(?<=Operation: ).*

Use Assign activity

 strApplication=System.Text.RegularExpressions.Regex.Matches(inputString, "(?<=Application: ).*")
 strAccount=System.Text.RegularExpressions.Regex.Matches(inputString, "(?<=Account: ).*")
 strOperation=System.Text.RegularExpressions.Regex.Matches(inputString, "(?<=Operation: ).*")

Regards,
Arivu

If the idea is to extract the 3 repeating rows which contains keyword Application, Account and Operation - you can use below pattern

Application:\s*\w+\sAccount\s:\s*\w+\sOperation:\s\w+

Note: It should come in the same same order

Hi @Harsha_Joshi
Welcome to the Community.

Here is one approach.

Regex:

LLM was used to generate the Regex only

"(?s)Application:\s*(?<Application>.*?)\s*Account\s*:\s*(?<Account>.*?)\s*Operation:\s*(?<Operation>.*?)(?:\s*Attribute:|$)"

It will give 2 matches with 3 groups each

The Output you want, will be in Match 2.

The Implementation:

Extract as individual Groups from second Match.

application = System.Text.RegularExpressions.Regex.Matches(inputString, "(?s)Application:\s*(?<Application>.*?)\s*Account\s*:\s*(?<Account>.*?)\s*Operation:\s*(?<Operation>.*?)(?:\s*Attribute:|$)")(1).Groups("Application").Value
account = System.Text.RegularExpressions.Regex.Matches(inputString, "(?s)Application:\s*(?<Application>.*?)\s*Account\s*:\s*(?<Account>.*?)\s*Operation:\s*(?<Operation>.*?)(?:\s*Attribute:|$)")(1).Groups("Account").Value
operation = System.Text.RegularExpressions.Regex.Matches(inputString, "(?s)Application:\s*(?<Application>.*?)\s*Account\s*:\s*(?<Account>.*?)\s*Operation:\s*(?<Operation>.*?)(?:\s*Attribute:|$)")(1).Groups("Operation").Value

Or

Extract the entire Match

match = System.Text.RegularExpressions.Regex.Matches(inputString, "(?s)Application:\s*(?<Application>.*?)\s*Account\s*:\s*(?<Account>.*?)\s*Operation:\s*(?<Operation>.*?)(?:\s*Attribute:|$)")(1).Value

The Output

  1. If you extract individual Groups:
    image

  2. If you extract entire match:
    image

If this solves your query, Do mark it as a solution
Happy Automation :star_struck:

Hey @Harsha_Joshi could you try this method

this will give to the exact value from the string input for most value just modify the inside pattern value from Account to Application or operation

cheers

Hey
I used the second approach of extracting entire match.
Thanks for you help

1 Like

Good to know.
Do mark it as a solution and close the thread.

Happy automation :star_struck:

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