IEnumerable <Regex Matches> to Datatable (best way to meaningfully extract data from multiple IEnumerables)?

I have a sequence where I am using ReadPDF and then the Matches activity with Regex to great success.

The output is coming out as ItemNamesRaw an IEnumberable variable for the first Matches activity and as QtyRaw which is an IEnumrable variable for the second Matches activity on the same PDF.

Currently I just have: For Each Item in the Variable converted to string and writing a line to be sure that they are all being read correctly.

I would like to have them append columns in an excel spreadsheet for inventory purposes, (all items to column A and all Qtys to Column B for example) but Iā€™m having trouble figuring out how to convert the IEnumerable into a DataTable as thatā€™s what is required to append a range.

1. Should I be aiming towards using Append Range to do this?

2. Is there a more meaningful and maybe flexible way to extract and collect the information within the IEnumerable so that I can access this information in the best way possible?

I donā€™t recall learning something specifically already that I can think of using, so maybe this is further ahead in the Academy than I am, (Iā€™m about 25%) but even being pointed in the right direction would be great.

As always thanks for all the help on these forums, itā€™s always quick and concise to get an answer and Iā€™m loving the community here.

Did you try ItemNamesRaw.CopyToDataTable? (you wonā€™t get intellisense, but you should import System.Linq)

if you need to bifurcate your Item and Quantity, build a datatable and probably you can bifurcate by length (eg >5) or contains hyphen (-) then add to different A or Add to column B.

Will think of more optionsā€¦

Where would I put ā€œItemNamesRaw.CopyToDataTableā€ ? ā€¦In an assign activity?

Also, what is System.Linq? and where would I use it?

Yes create a database variable and assign ItemNamesRaw.CopyToDataTable to that variable.

To Import System.Linq, after Variable,Arguements tabs you see Import Tab, in the search bar type System.Linq and add it to your project.

2 Likes

Getting this error when trying to use the .CopyToDataTable

Itā€™s not popping up to autopopulate like commands usually do.

As I understand then, .Linq imported will give me more commands to use? Including .CopyToDataTable?

I imported the System.Linq and System.Linq.Expressions and Iā€™m still getting that same error though.

Do I need to do anything else to be able to use those expressions?

Sorry I did not notice that your Inumerable is Regex Match type, it works only with DataRow type.I will give you another solution.

Thank you!
*Waits patiently
:thinking:

Your item and quantity are mapped? Or Random?

1 Like

They are Mapped, corresponding to each otherā€¦ Is that what you mean?

Yes sir.just a thought on what basis are you getting regex match? Can you concatenate item quantity at regex level itself?

Item: (?<=Article Number: ).{13}([^\n\r]*)
Qty: \d+(?= piece ).{1}

ā€¦ Even if I get them both from regex and concatenate them, Iā€™m still looking at how Iā€™m going to get the data out of an IEnumerable of Regex though right?

If I got them into just one, concatenatedā€¦ how would I get to that data? Isnā€™t it the same problem Iā€™m having or could I do something else with that?

This should give an idea. Good luck.

matches.xaml (9.5 KB)

9 Likes

That was exactly what was needed. :clap:

Thank you very much, Iā€™ve got everything running correctly with my data.

It was very helpful for you to make a template for me, and I can understand the processes used now. Really great job and thanks again and kudos!!

Hey, I saw your solution and I am looking for something similar I think, but I donā€™t understand the file you send.

I use a matches activity with the following regex pattern: ā€œ\s[0-9]{23}\sā€, to find all strings on a page that are 23 digits long. Now I want to filter the results so there is only unique results. I thought Iā€™d do that by putting them in a data table or excel or something and then further filter, but I canā€™t figure out how to get the collection of matches into a data table/excel

Help would be much appreciated!
Thanks :slight_smile:

Hi vvaidya,

Your solution is what i was looking for. But still i couldnt convert IEnumerable to Datatable.

At ā€˜Add Data Rowā€™ step, I am getting the below error message. Kindly help

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.ArgumentException: Unable to cast object of type '<CastIterator>d__97`1[System.Text.RegularExpressions.Match]' to type 'System.IConvertible'.Couldn't store <System.Linq.Enumerable+<CastIterator>d__97`1[System.Text.RegularExpressions.Match]> in Email Ids Column.  Expected type is Int32. ----> System.InvalidCastException: Unable to cast object of type '<CastIterator>d__97`1[System.Text.RegularExpressions.Match]' to type 'System.IConvertible'.
   at System.Data.Common.Int32Storage.Set(Int32 record, Object value)
   at System.Data.DataColumn.set_Item(Int32 record, Object value)
   --- End of inner ExceptionDetail stack trace ---
   at System.Data.DataColumn.set_Item(Int32 record, Object value)
   at System.Data.DataTable.NewRecordFromArray(Object[] value)
   at System.Data.DataRowCollection.Add(Object[] values)
   at UiPath.Core.Activities.AddDataRow.Execute(CodeActivityContext context)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Did you ever get this working? I hope so!! but if not lemme know and I can show you how to use Matches to find what you want, and you donā€™t need to dump the data into an excel first or anything.

Hi Vvaidya,

Thank you very muchā€¦
Your solution saved my day.

Thanks for this, almost what I needed but now I am now going mad! How do I create the table if the item and quantity are not mapped and there are items where the quantity is empty