Remove punctuation from string with Regex

前端 未结 2 936
栀梦
栀梦 2021-02-04 01:52

I\'m really bad with Regex but I want to remove all these .,;:\'\"$#@!?/*&^-+ out of a string

string x = \"This is a test string, with lots of: punctuations;         


        
2条回答
  •  迷失自我
    2021-02-04 02:42

    First, please read here for information on regular expressions. It's worth learning.

    You can use this:

    Regex.Replace("This is a test string, with lots of: punctuations; in it?!.", @"[^\w\s]", "");
    

    Which means:

    [   #Character block start.
    ^   #Not these characters (letters, numbers).
    \w  #Word characters.
    \s  #Space characters.
    ]   #Character block end.
    

    In the end it reads "replace any character that is not a word character or a space character with nothing."

提交回复
热议问题