This should have been asked here, but apparently it hasn't. How to delete everything from a string except alphanumeric characters and spaces? For example,

str = "This is, my string number 5!"

I want it to be "This is my string number 5"

  • 2
    $\begingroup$ StringReplace["This is, my string number 5!", Except[LetterCharacter | DigitCharacter | " "] -> ""] gives the output you want (but doesn't have special characters). $\endgroup$
    – Ben Izd
    Commented May 22, 2022 at 8:59

2 Answers 2


One way is to use RegularExpression

str = "This is, my string number 5!";
reg = RegularExpression["[A-Za-z0-9 ]"];
StringJoin[StringCases[str, reg]] // InputForm


"This is my string number 5"

The " " is for any space, and A-Za-z0-9 stuff is for alpha numeric.

str = "This is, my string number 5!"
  , (DigitCharacter | LetterCharacter | WhitespaceCharacter)

StringDelete[str, PunctuationCharacter]


"This is my string number 5"


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.