Convert System.Collection.Generic.IEnumerable<System.Text.RegularExpressions.Match> into a String

Hi,

I have used Matches to extract all of the urls from within a string and Matches outputs a variable type; System.Collection.Generic.IEnumerable<System.Text.RegularExpressions.Match>. How do I convert the System.Collection.Generic.IEnumerable<System.Text.RegularExpressions.Match> variable into a String?

Thanks, Katie

Hello @Katie_Vooght

You need to place for each and inside for each you can assign a variable to make that string

Eg:

image

Mark as solution if this helps for you

Thanks

1 Like

@Katie_Vooght
accesing the first: yourMatchVar(0).toString
iteration as shown by @ksrinu070184
bringing all into an Array / List:
yourMatchVar.Select(Function (x) x.toString).toArray / .toList

1 Like

Thanks both.

ksrinu070184’s way has resulted in getting just the last url in the list of all urls that I want. I need to get all of the urls of which there are over 20.

I am not sure what you mean by yourMatchVar.Select(Function (x) x.toString).toArray / .toList? Could you paste a screenshot of UiPath with this in context please?

@Katie_Vooght
your Matches Activity will have an output. As I dont know the variable name so yourMatchVar is the placeholder for this name. Just replace it by your variable name

when you change @ksrinu070184 approach and add an additional log message in which you log out the variable

Do you mean like this? I still only get one url and not a list…

@Katie_Vooght
give me some little time I will do some starter help and will share it with you

1 Like

@Katie_Vooght
Find starter help here:
AllMatchesToArray-List.xaml (7.2 KB)

Thanks so much this is really useful.

One (hopefully) last question, do you know why I am getting an exclamation mark on ‘Assign’ in the screenshot below?

When I hover over it it says that ‘Cannot assign from type ‘System.String’ to type ‘System.String’ in Assign activity ‘Assign’.’

Your Matches variable is my ‘Website_Links’ variable

@Katie_Vooght
ensure you have configured the datatype of MatchValues2 to String Array

I have replicated your example into my workflow and I still only get one link come through :frowning:

@Katie_Vooght
can you share your XAML with me? If not then:

decide which approach you want to use:

  • for each or LINQ

  • remove the parts from not needed approach

  • check required variables and datatypes definitions

  • debug stepwise and analyse the flow

I have type argument as object in ‘For Each’, would this make a difference? If i change it to ‘System.text…’ like what you have I get the exclamation mark.

@Katie_Vooght
please change to
grafik

ensure following:
grafik

ensure currentMatch is of datatype String

That works however still only the last link extracted and not all of them…

Sorry so many questions, you have been so helpful!!

@Katie_Vooght
you can do following:

  • debugging and inspect your Website_Links (the Matches)
  • checking the log messages
  • checking the MatchValue/2 contents at end of worklflow

But kindly note: each loop overwrites currentMatch with the current Link from Loop. So do not misinterpretate when you refer later on this

In the first method with For each, what is the final variable with all of the urls? Is it MatchValue?

If I use ‘Write text file’ activity with currentValue I get just the last url. It would make sense to use MatchValue however that does not work due it not be declared / protection levels.

@Katie_Vooght
Can you Share your xaml?

I have attached a simplified version but keeping the key bits! Main_Example.xaml (12.5 KB)

You will see that the ‘Website Lists Test’ txt file that it outputs only has one link.

@Katie_Vooght

But kindly note: each loop overwrites currentMatch with the current Link from Loop. So do not misinterpretate when you refer later on this

as already mentioned: the currentMatch variable stores only the Match from Loop.
Changes:

  • grafik
  • removing some unused variables
  • adopting some scopes

Unfortunately I was blocked to run and test it. But it should work. Find reworked XAML here:
Main_Example.xaml (12.5 KB)

1 Like