How to get text inside brackets

Hello,

i have output like below
Color [Red]
i need to get only “Red”
help me on same .
expected output -
Red

Assign Activity
strText =
System.Text.RegularExpressions.Regex.Match(YourTextVar,"(?<=\[).*?(?=\])").Value

[CheatSheet] - System.Text.RegularExpressions | RegEx - News / Tutorials - UiPath Community Forum

1 Like

@Mathkar_kunal

use regex

System.Text.RegularExpressions.Regex.Match(strValue, “(?<=[)(.*)(?=])”).ToString

if you find it helpful, please mark it as solution

1 Like

Hi @Mathkar_kunal

result = System.Text.RegularExpressions.Regex.Match(yourString, “[(.*?)]”).Groups(1).Value

1 Like

Hello @Mathkar_kunal ,

Store the text where you are getting the output like:

“i have output like below
Color [Red]
i need to get only “Red”
help me on same .
expected output -
Red”

in a variable named ‘inputText’, and then pass this variable into the Regex expression.

Regex:-

System.Text.RegularExpressions.Regex.Match(inputText, “(?<=[).+?(?=])”).Value

1 Like

Hi @Mathkar_kunal

Another approach,

Regards,

I will chime in with an answer if there happens to be a case where your input contains multiple brackets and you would like to output each one of them.
For example:
Input: The apple is [red], the banana is [yellow] and the pear is [green].

We already have the correct answer on how to get “red”, see the answer from ppr.
To output red, yellow and green we need to read the values into a MatchCollection that we can later on loop through with a For Each-activity.

Assign a string with the input, assign a new MatchCollection-variable with the value: System.Text.RegularExpressions.Regex.Matches(inputvariable, "(?<=[)(.?)(?=])")* and then loop through the collection with a for each where lists of items is the variable name for your collection.

1 Like

nice activity , but i cant find it in activities.
any package needs to be install ? i will use this later

UiPath.System.Activities

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