You can filter by special character by removing alphanumerics.
extension String { func removeCharacters(from forbiddenChars: CharacterSet) -> String { let passed = self.unicodeScalars.filter { !forbiddenChars.contains($0) } return String(String.UnicodeScalarView(passed)) } } let str = "£€₹jetztabfromnow12" let t1 = str.removeCharacters(from: CharacterSet.alphanumerics) print(t1) // will print: £€₹ let t2 = str.removeCharacters(from: CharacterSet.decimalDigits.inverted) print(t2) // will print: 12 Updated 1:
var str = "£3,95SS" str = str.replacingOccurrences(of: ",", with: "") let digit = str.removeCharacters(from: CharacterSet.decimalDigits.inverted) print(digit) // will print: 395 let currency = str.removeCharacters(from: CharacterSet.alphanumerics) print(currency) // will print: £ let amount = currency + digit print(amount) // will print: £395£3,95 Update 2:
let string = "£3,95SS" let pattern = "-?\\d+(,\\d+)*?\\.?\\d+?" do { let regex = try NSRegularExpression(pattern: pattern, options: []) if let match = regex.firstMatch(in: string, range: NSRange(location: 0, length: string.utf16.count)) { let range = match.range let start = string.index(string.startIndex, offsetBy: range.location) let end = string.index(start, offsetBy: range.length) let digit = string.substring(with: start..<end) print(digit) //3,95 let symbol = string.removeCharacters(from: CharacterSet.symbols.inverted) print(symbol) // £ print(symbol + digit) //£3,95 } else { print("Not found") } } catch { print("Regex Error:", error) }