Hi, i'm new to ios development. Can someone give some example to how to use the Metacharacters in RegularExpression pattern?
var regex = try! NSRegularExpression(pattern: "[\D]", options:[])
this shows invalid escape sequence in literal error
Hi, i'm new to ios development. Can someone give some example to how to use the Metacharacters in RegularExpression pattern?
var regex = try! NSRegularExpression(pattern: "[\D]", options:[])
this shows invalid escape sequence in literal error
You should escape the escape:
var regex = try! NSRegularExpression(pattern: "[\\D]", options:[])
You could also try using
class func escapedPattern(for string: String) -> String
https://developer.apple.com/documentation/foundation/nsregularexpression/1408386-escapedpattern
A good option here is to use Swift ‘raw string’ syntax. For example:
print("<\t>")
// < >
print(#"<\t>"#)
// <\t>
In the first case the \t
is interpreted as a tab whereas in the second it’s interpreted as \t
.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"