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$
0

1 Answer 1

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$
3
  • $\begingroup$ What is PunctuationCharacter ? $\endgroup$
    – eldo
    Oct 27, 2015 at 10:53
  • $\begingroup$ @eldo I updated the answer. $\endgroup$
    – Szabolcs
    Oct 27, 2015 at 10:57
  • 6
    $\begingroup$ ...and of course, regex: StringReplace[list, RegularExpression["[[:punct:]]"] -> " "]. $\endgroup$ Oct 27, 2015 at 10:59

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.