Hi,
I have a set of data that is extracted using the RegEx function.
Now I want to export it to an excel file. But when I use this write CSV activity I am getting an error as it it not in datatable format.
May I know how can I extract the regex output to a csv file?
- Use Build DataTable activity to create new DataTable with required columns. Lets say dtData.
- And then use Add DataRow activity and pass extracted data. This activity helps to add data to the DataTable.
- Finally use Write CSV activity and pass above dtData datatable to write into CSV file.
- First you have to load the each set of data that was extracted using regex to data table
- Then, the data table can write to CSV
- Use the link below to know how to add the data to a data table
Thanks @ushu ,
Just want to check with you, is there a way to add it automatically. For every run, I will get a different set of output from regex. Do I need to add this data each time to data table?
@John133 Yes, every time the data has to be updated in the data table. But adding to a data table is pretty easy
- Take Add data row activity
- Pass the variable that contains the extracted data in the Array Row field. It simple adds that data to a data table
- Finally the same data table can write to CSV. Follow the above doc it would help you
Another Approach:
If you don’t want to use the data table, you can write to excel cell once the each set of data got extracted. But opening excel multiple times is not a good approach (you can try according to your requirement)
@ushu ,
It will be a list of 10 or more names.
For eg:
T0WJDDJ
T02032S
TOOOSZ
.
.
.
This output result is saved in strRegexOutput variable which is in IEnumerable type.
May we ask you to share some sample text (used for the regex input) and the used Regex Pattern. Your case has the potential of getting applied a shortcut. Thanks
Hi @ppr ,
Regex input data ,
$$_BEAN_SWLOG
@ 1 2345782I
! 1 1 1;1 SSS W0H0
! 1 1 1;1 SAH W0S0
! 1 1 1;1 S33 WBQE
! 0 1;1 S4H0 WERI
! 0 1;1 SH0 WSDF
! 0 1;1 SH0 W8ZS
reg ex expression: “(?<=! [1] [1] [1];[1] \w+ )\w+”.
I am capturing data that starts with ‘W’ at each line.
But only condition is that after " ! " , it should be 1.
So data needed is from line 3,4,5. (WOHO,WOSO,WBQE)
When the CSV is only about 1 column represented by the output from regex then we can write it more direct
Assign Activity:
myMatches = Regex.Matches(strText, strPattern)
Assign Activity
strMyCSV = String.Join(Environment.NewLine, myMatches.Cast(of Match).Select(Function (m) m.Value))
And finaly we use strMyCSV with the Write text file activity
We can adopt and modify as needed (header lines…)
Another strategy could be to pass the extracted data to a generate datatable activity and get generated from text the datatable
@ppr , Hi Peter,
Is “strPattern” an output variable from matches activity?
strPattern is a String Variable. We assign on its value the Regex Pattern. Later we use this pattern via the variable within the Regex.Matches
For some trainings have a look here
[CheatSheet] - System.Text.RegularExpressions | RegEx - News / Tutorials - UiPath Community Forum
and do some prototypings within the debug panels e.g. immediate panel
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum
@ppr ,
I am getting this error.
Also, the sample text used for Reg ex input is extracted from previous steps and it is saved as txt file in a local drive.
the datatype of Mymatches is MatchCollection and not String. Just adopt it within the variable panel