Swift Language NSRegularExpression in Swift Replacing Substrings


Patterns can be used to replace part of an input string.

The example below replaces the cent symbol with the dollar symbol.

var money = "¢¥€£$¥€£¢"
let pattern = "¢"
do {
    let regEx = try NSRegularExpression (pattern: pattern, options: [])
    let nsString = money as NSString
    let range = NSMakeRange(0, nsString.length)
    let correct$ = regEx.stringByReplacingMatches(in: money, options: .withTransparentBounds, range: range, withTemplate: "$")
} catch let error as NSError {
    print("Matching failed")
//correct$ = "$¥€£$¥€£$"