3

I have some code to remove all punctuation characters:

mysentence.components(separatedBy: .punctuationCharacters).joined().components(separatedBy: " ")

Now, I am trying to add an exception for the dash character "-". In other words, I want to remove all punctuation EXCEPT for dashes.

It looks like in Java this can be done with replaceall using a regular expression (which admittedly I don't know how to use). How might I do this in Swift 4?

Coltuxumab
  • 615
  • 5
  • 16

1 Answers1

4

Maybe you remove dash - from CharacterSet like:

var set = CharacterSet.punctuationCharacters
set.remove(charactersIn: "-")
Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64