Regex, SubString or Linq?

I have a datatable containing text in each row. I need to split it in to individual words where there are any spaces, hypens or special characters. If there are, remove the spacial character & print the word

For example:
Test1 Test2- tes’t te-st

Should become:

Test1
Test2
tes
t
te
st

At the moment i loop through the datatable & ‘if the row contains " ", “-”, "’"’ , pass to a strig array then loop through again, but is there a more elegant way to do it through linq or something?

Thanks

Hi @qwerty1 ,

Could you Check with the below Expression :

Regex.Split("Test1 Test2- tes’t te-st","[^A-Za-z0-9]").ToArray

Make sure the namespace System.Text.RegularExpressions is imported.

3 Likes

@qwerty1

You can try this

System.Text.RegularExpressions.Regex.Split(str,"\W").Where(function(x) Not String.IsNullOrEmpty(x)).ToArray

image

The Where part is to remove any empty entries from the output array

Hope this helps

cheers

2 Likes

HI,

How about the following?

System.Text.RegularExpressions.Regex.Split(yourString,"[_\W]+")

Regards,

1 Like

Really great solutions all around, so thank you all for your suggestions. I’ve managed to implement @supermanPunch suggestion, but all the contributions worked perfectly, so thank you for your quick replies.

1 Like

@Anil_G @supermanPunch @Yoichi

Can you extend your help with this please?
I have two lists that i need to iterate through and validate.

ListOne of type string contains:
Test1, Test2-, tes’t, te-st

ListTwo of type string contains:
test3, tes-t4, te’st5

My plan was to create a list variable of type string with {“ListOne”, “ListTwo”} then loop it and invoke a workflow to loop through the list using the regular expression to remove special characters. I cannot seem to do that as it loops the string ‘ListOne’ and productes L, i, s, t, O, n, e. I cannot seem to create a List (of string) to loop {ListOne, ListTwo} as i receive an error that it cant assign from generic list [system string] to generic list [system string]

Is there another way I should approach this?

Hi @qwerty1 ,

We will provide a Quick Suggestion, as this new requirement tends to be away from the original requirement, We would want to ask you to create a Separate Topic on this. However, This should be easier to implement.

You would need to create List of Lists :

listOfLists = new List(Of List(Of String)) From {ListOne,ListTwo}

If this query tends to move still further with further queries would request you to create a new topic.

1 Like

@qwerty1

So you are already splitting the string based on special characters right…That anyways is removing the special characters from string…and giving you an array of string…

Now what you want to do…is Listone of type list of string or string?

If both are lists then use

then Listone + listtwo will concatenate the lists together and create one and now to remove the special characters from each item

(Listone+ListTwo).Select(function(x) System.Text.RegularExpressions.Regex.Replace(x,"\W","")).ToList()

cheers

cheers

Thank you however, i’m unable to concatenate as these lists after being updated through the regex, the items need to be add to separate collections - depending on the orignal list source.

@qwerty1

If you do a .ToList they would be casted to list and then be concatenated…

Preferably as this is different topic feel free to create another thread if this doesnot resolve yet

cheers

1 Like

@supermanPunch @Anil_G As requested i have created a new thread to discuss further: Pass two lists through one validation

1 Like

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