How to remove all digits from a string using regex in Swift

1 Answer

0 votes
import Foundation

var s = "abs322kl59po@$d0057qn8"

// Create a regular expression to match digits
let digitsRegex = try! NSRegularExpression(pattern: "[0-9]", options: [])

// Use stringByReplacingMatches to replace digit characters with an empty string
s = digitsRegex.stringByReplacingMatches(in: s, options: [], range: NSRange(location: 0, length: s.count), withTemplate: "")

print(s)


 
/*
run:
 
absklpo@$dqn

*/

 



answered Dec 10, 2024 by avibootz
...