Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove all non alphanumeric word from a List<string>

Tags:

c#

regex

How do I remove all non alphanumeric words from a list of strings (List<string>) ?

I found this regex !word.match(/^[[:alpha:]]+$/) but in C# how can I obtain a new list that contains only the strings that are purely alphanumeric ?

like image 489
LeMoussel Avatar asked Oct 15 '15 13:10

LeMoussel


3 Answers

You can use LINQ for this. Assuming you have a theList (or array or whatever) with your strings:

var theNewList = theList.Where(item => item.All(ch => char.IsLetterOrDigit(ch)));

Add a .ToList() or .ToArray() at the end if desired. This works because the String class implements IEnumerable<char>.

like image 75
Konamiman Avatar answered Sep 28 '22 23:09

Konamiman


  Regex rgx = new Regex("^[a-zA-Z0-9]*$");
  List<string> list = new List<string>() { "aa", "a", "kzozd__" ,"4edz45","5546","4545asas"};
  List<string> list1 = new List<string>();
  foreach (var item in list)
  {
     if (rgx.Match(item).Success)
     list1.Add(item);
  }
like image 31
Ghassen Avatar answered Sep 28 '22 23:09

Ghassen


With LINQ + regex, you can use this:

list = list.Where(s => Regex.IsMatch(s, "^[\\p{L}0-9]*$")).ToList();

^[\\p{L}0-9]*$ can recognise Unicode alphanumeric characters. If you want to use ASCII only, ^[a-zA-Z0-9]*$ will work just as well.

like image 40
wingerse Avatar answered Sep 29 '22 00:09

wingerse