0

I have a List and wish to clean it up by removing various characters. I decided to try Lambda and Regex for the first time but have fallen flat on my face.

foreach (var str in listResults)
    {
       listResults.Select(c => {c = removePunctuation(c); return c;}).ToList(); 
    }


private string removePunctuation(string str)
    {
        return Regex.Replace(str, @"\t|\n|\r|.|,|:|;|!", " ");
    }
NadJ
  • 13
  • 5
  • not the issue, but `listResults.Select(c => {c = removePunctuation(c); return c;}).ToList(); ` => `listResults.Select(c => removePunctuation(c)).ToList();`, and remove the `foreach` ! The whole expression returns a list of the transformed values. – Pac0 May 06 '20 at 13:44
  • 1
    `.` matches any char, you need to use `\.` to match a dot. You should actually use a character class for punctuation, `\p{P}` - `return Regex.Replace(str, @"\p{P}", " ");`. Or, if you also want to match symbols, `Regex.Replace(str, @"[\p{S}\p{P}]", " ")`. If you need to shrink 1+ consecutive punctuation chars with a single space, use `+` after the character class, `Regex.Replace(str, @"[\p{S}\p{P}]+", " ")` – Wiktor Stribiżew May 06 '20 at 13:44

0 Answers0