REGEX Formula


is there any way i can show more than 1 match on my uipath as currently it only shows the first match

Hi @Kian,

For that you can use Matches activity OR Matches method with following method in assign activity,

matches = System.Text.RegularExpressions.Regex.Matches(StringVar,"RegexPattern")

here matches var in left side of assign —> should be of type System.Text.RegularExpressions.MatchCollection
StringVar —> is of type System.String, holding input string,
don’t forget to put regex pattern in quotes. here —> “RegexPattern”

to display each value… we need For Each activity, specify metaches var in it with type argument as
—> System.Text.RegularExpressions.Match
inside messageBox with value as —> item.ToString

here’s workflow screenshot for your better understanding.

it does not really work as i have tried.
this is my workflow can you help to see whats wrong.Sequence.xaml (19.2 KB)

@Kian,
Check this I’ve made changes in it.
Sequence.xaml (18.4 KB)

  1. you have used StringVar as it is

You should use your input string variable no.

  1. matches variable should be of type `System.Text.RegularExpressions.Match —> MatchesCollection

@Kian, I’ll suggest you to check this sample workflow, check input, run it and check output, you’ll get exact idea that how it works.

Test1.xaml (6.3 KB)

Then use your only 1 input with your regex pattern and check for output, if it works good, then use above logic in your workflow.

Hello @Kian ,

You should use Multiline option. By the way, a lookbehind with variable character number seems fishy… could you please try the following?:

  • Import System.Text.RegularExpressions

  • Assign (String)
    pattern = "^.+?(?=\s-)"

  • Assign (MatchCollection)
    matches = Regex.Matches(myText, pattern, RegexOptions.Multiline)

  • ForEach match in matches (element type is Match)

    • LogMessage
      match.Value.ToString