You need to use the bindings, ^(start of string binding) and $(end of string binding) range(of:options:range:locale:), passing the parameter .regularExpression:
import Foundation
let phoneNumber = "123-456-789"
let result = phoneNumber.range(of: "^\\d{3}-\\d{3}-\\d{3}$", options: .regularExpression) != nil
print(result)
Or you can pass an array of parameters [.regularExpression, .anchored]where .anchoredonly the string will be bound at the beginning, and you can skip ^, but still $need to bind to the end of the string:
let result = phoneNumber.range(of: "\\d{3}-\\d{3}-\\d{3}$", options: [.regularExpression, .anchored]) != nil
Watch Swift Demo Online
, NSPredicate MATCHES :
ICU v3 ( . ICU ).
MATCHES , ( , Swift 3):
let pattern = "\\d{3}-\\d{3}-\\d{3}"
let predicate = NSPredicate(format: "self MATCHES [c] %@", pattern)
let result = predicate.evaluate(with: "123-456-789")