How to Split Visible text that uses regex

Hi Experts

How would I split a string which I made a regex for and write it out on a excel worksheet.


To split a string, you need to use the .Split() function. For example, in strData.Split(“,”.ToCharArray), you have split with the “,” character and the output is a list. So be careful to have declared a list at the left.

To write it in an Excel, it depends what you wants exactly. You can access the elements like you’ll do for a list.

Good luck!

Best regards

1 Like

the .Split is giving me an error because it is not an ienumerable

As shown in the screenshot

Ok, you mean that your variable " valueSet " is not a string. Is your “Matches” activity giving you more than 1 result? If yes, then you’ll need to iterate through the results and do the split for each element. Use a “For Each” activity with TypeArgument " System.Text.RegularExpressions.Match ".

1 Like

The matches activity gives me 1 string

If your Regex gives you always one result, then you don’t need a loop. Just use : valueSet.ElementAt(0).ToString().Split(“;”.ToCharArray) . Replace the “;” with the character you want to do the split.

1 Like

Giving me this error

Is splitValueCkNo a list? You have this error because there is a difference of types. At the right you have a list because when you do a split, it separates the string and the output is a list of strings.

Please double check if your variable has the right type, it should be “System.String”.

Good luck

1 Like

what do I search in browse for a system.string

Nevermind I got it! I have one more question. How would I write that to a write range since its an ienumerable

If your concern is only to write in an Excel, you can use the Write Cell activity and loop through each element of your IEnumerable, with a counter to go to the next row (be careful to initialize the counter with the value 2 because there is a shift between UiPath & Excel).

If you want to use Write Range activity, then you need to convert your IEnumerable to a datatable. I’ll probably use the “Add Data row” activity inside a loop which loops the IEnumerable. Please look at the example below:

1 Like

Is it ok if you can show me the add data row build data table method?

At the top (not shown in screenshot) I made a datatable with 1 column how would I use the regex I split and write it

Yes of course. This is how it works:
When you want to use a datatable, you always need to build it by using the Build Data Table activity.

When you click on “DataTable…”, you can add the columns you want by clicking on the “+” button, here in the example I just declared one column corresponding to your Regex.

After building your datatable (don’t forget to put the output name of your datatable), you can iterate through your regex, and use the Add Data Row activity, as below:

As you can see, I have put " {item}" in the ArrayRow part.

This example shows you how to write it line by line, I hope this is what you need.

1 Like