Regex.Replace on String Array

Hello,

I have made a data scraping process that takes data from a word document and outputs it to Excel. The scraping of the data is fine, but I have noticed that a lot of the values are followed by \r, \n, \a,or \t. I have got a Regex.Replace working on one String value.

The Regex I am using is:

System.Text.RegularExpressions.Regex.Replace(StringName,"\r|\a|\t|\n","")

image this is the value of a string without using regex

image this is the value using regex

I tried putting an Array of Strings into where the String Variable name goes, but it gave me a validation error, saying that 1-dimensional array of string can’t be converted to string.

Can I modify the Regex to replace everything in the array? If so, how do I do that? Any help is appriciated

You will have to loop through the array with for each activity & apply regex to each item in the array (inside the loop).

Regards,
Nithin

1 Like

Hey @Nithin_P,

Something like this? TypeArgument is set to string

image

Hi @william.coulson,

Exactly !

You can now add item to a List if required. So that you can access it later.

Regards,
Nithin

@william.coulson
working with for each is recommandable, as the implementation can be very well inspected and tracked.

Still you will have an option for following oneliner, used within an assign without for each:
OutputArray.Select(Function (s) System.Text.RegularExpressions.Regex.Replace(s,“\r|\a|\t|\n”,“”).toArray (or toList if you prefer)
ther return will be a string array

about the search pattern maybe with \s|\a it can be shortened

1 Like

Hi @Nithin_P,

I used message boxes to see the values and the Regex is working, but it is not outputting them correctly on the Excel sheet, as they still have Character Escapes following them

You can create a new array (outside the loop) & add item to it in the loop after the assign activity. Soething like this NewArray(LoopIndex) = item. The original array won’t be updated automatically.

Regards,
Nithin

1 Like

Hey @Nithin_P,

I have made this

But it returns Object reference not set to instance of object as this new array is null

Please follow these steps. New array has to be declared outside the loop and using the loop index the items have to assigned.

Regards,
Nithin

2 Likes

Hey @Nithin_P,

Declaring the Array has worked, loops through the data fine and outputs the values to Excel with no issues. Thank you for your help!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.