Regular Expression to replace non alpha characters with spaces

前端 未结 2 1883
栀梦
栀梦 2021-01-22 13:32

I have been trying to build a regular expression but haven\'t been able to get one specific condition to work.

I want a regex to remove all non alpha characters with the

2条回答
  •  一生所求
    2021-01-22 14:04

    // Skip over '-', grab non-word characters or the ' -' sequence to replace
    string pattern = @"(?!-)(\W| -)+";  
    string replacement = "";
    Regex regex = new Regex(pattern);
    string result = regex .Replace("Replace - this *@#&@#* string-already", replacement);
    

    The (?!-) is a zero-width negative lookahead assertion that will skip over the '-' symbol... the second group will match it if it's preceded by a space.

    If you're trying to substitute a space instead of completely removing the characters, just change to

    string replacement = " ";
    

    the pattern is greedy, so it will replace groups of non-word characters with a single space.

提交回复
热议问题