How to make [ ] as optional in regex

Hi,
I’m new to UiPath and very new to regex.
Need to retrieve values of OnError, Name and Path. Sometimes the values of Name and Path will be in ’ ’ and sometimes in-between ‘[ ]’.

Attaching the Screenshot and text file for your reference.
Thanks in Advance!

sampleText.txt (272 Bytes)

1 Like

Hi

Hope these expressions will help you in this

If your input is in a string variable named Strinput

Then in a assign activity

Str_OnError = System.Text.RegularExpressions.Regex.Match(strinput.ToString,”(?<=OnError=\W).*(?=\WPro)”).ToString

Str_Name = System.Text.RegularExpressions.Regex.Match(strinput.ToString,”(?<=Name=\W).*(?=\WPat)”).ToString

Str_Path = System.Text.RegularExpressions.Regex.Match(strinput.ToString,”(?<=Path=\W).*(?=\W\s)”).ToString

Cheers @Kavya_Mamidishetti2

@Palaniyappan So I think I can use OR(|) and combine these expressions and convert these values into list.

1 Like

Fine

In that case

Use like this

List_output = System.Text.RegularExpressions.Regex.Matches(strinput.ToString,”(?<=OnError=\W).(?=\WPro)|(?<=Name=\W).(?=\WPat)|(?<=Path=\W).*(?=\W\s)”).ToList()

Where list_output is a list of string variable

Few * gets disappeared here
So as you said we need to keep a | (or) and mention as matches instead of match @Kavya_Mamidishetti2

Cheers @Kavya_Mamidishetti2

@Palaniyappan Thanks for the response but it is throwing an error though my input is of type String and output variable is of type List(Of string).

1 Like

Pls try like this

Listoutput = (From a in System.Text.RegularExpressions.Regex.Matches(strinput.ToString,”(?<=OnError=\W). (?=\WPro)|(?<=Name=\W). (?=\WPat)|(?<=Path=\W).*(?=\W\s)”) select Convert.ToString(a)).ToList()

Which is like

Listoutput = (From a in yourexpression select Convert.ToString(a)).ToList()

Cheers @Kavya_Mamidishetti2

1 Like

@Palaniyappan Getting these two values as output

Screenshot (112)

1 Like

Can I have the text if possible
I can give you the expression

Cheers @Kavya_Mamidishetti2

Regex:
(?<=\bName=‘|\bPath=’|\bOnError=‘)(?:[?{?)(.*?)(?=]?}?’)


And refering to group 1

Kindly note also { } was to handle

In general it is XML and should also be processed with help of XML options / API

@Palaniyappan yes sure.
Here it is
sampleText.txt (272 Bytes)

@ppr Thanks for the response.It didn’t throw any error and didn’t give any output too as I am assigning your pattern to a list variable.

List_output=System.Text.RegularExpressions.Regex.Matches(strinput,“(?<=\bName=’|\bPath=’|\bOnError=’)(?:[?{?)(.*?)(?=]?}?’)”).Cast(Of Match).Select(Function(n) n.Value.ToString).ToList

@Kavya_Mamidishetti2
Ensure that there is no Copy / Paste issue e.g. for ’ which has to be '

will have a look later on it. Unfortunaltely accesing the group is not implemented in your statement. Can I test later with the string from last text file? Is this sample still valid?

1 Like

@ppr :slight_smile: okay.I take care of Copy/Paste issues. You can take the recent text file for reference.

grafik

Pattern: "(?<=\bName='|\bPath='|\bOnError=')(?:[\[\{]?)(.*?)(?=\]?\}?')"

@ppr Thanks. It is working fine.

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