Home > database >  Replace special characters and spaces with empty string with REGEX
Replace special characters and spaces with empty string with REGEX

Time:10-13

I am looking to take a string from an input text box and remove all special characters and spaces.

e.g. Input: ##HH 4444 5656 3333 2AB## Output: HH4444565633332AB

CodePudding user response:

Let's define what we are going to keep, not what to remove. If we keep Latin letters and digits only we can put

string result = Regex.Replace(input, "[^A-Za-z0-9]", "");

or (Linq alternative)

string result = string.Concat(input
  .Where(c => c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' c >= '0' && c <= '9'));

CodePudding user response:

If dealing with unicode to remove one or more characters that is not a letter nor a number:

[^\p{L}\p{N}] 

See this demo at regexstorm or a C# replace demo at tio.run

  • \p{L} matches any kind of letter from any language
  • \p{N} matches any kind of numeric character in any script
  • Related