2
$\begingroup$

I have a list of strings. Some of them contain commas, single quotes, etc. Is there a quick way to replace those characters by blanks? My list looks something like this:

list = {"string1", "bla,bla", "more'stuff", "and more;stuff"}

cheers

$\endgroup$
8
$\begingroup$

Use

StringReplace[list, PunctuationCharacter -> " "]

(* {"string1", "bla bla", "more stuff", "and more stuff"} *)

PunctuationCharacter is new in version 10.3. The 10.x updates received many new functions for text processing. In older versions I would use an explicit list of possible punctuation characters, i.e.

punctuation = Alternatives@@Characters[",.?:;'\"!-"]

I'm pretty sure I missed some.

StringReplace[list, punctuation -> " "]
(* {"string1", "bla bla", "more stuff", "and more stuff"} *)
$\endgroup$
  • $\begingroup$ What is PunctuationCharacter ? $\endgroup$ – eldo Oct 27 '15 at 10:53
  • $\begingroup$ @eldo I updated the answer. $\endgroup$ – Szabolcs Oct 27 '15 at 10:57
  • 5
    $\begingroup$ ...and of course, regex: StringReplace[list, RegularExpression["[[:punct:]]"] -> " "]. $\endgroup$ – J. M. will be back soon Oct 27 '15 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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