Add this for supporting regex matches for string:
extension String {
mutating func stringByRemovingRegexMatches(pattern: String, replaceWith: String = "") {
do {
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, self.characters.count)
self = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith)
} catch {
return
}
}
}
And then:
var str = ["width 32.3", "Length 61 1/4", "height 23 4/5", "measure 5.23"]
var vreturn:[String]=[]
for var s in str {
s.stringByRemovingRegexMatches(pattern:"[a-zA-Z]", replaceWith: "")
vreturn.append(s)
}
print( vreturn)
Source : Swift replace substring regex