Swift Language Special Characters


Example

To match special characters Double Backslash should be used \. becomes \\.

Characters you'll have to escape include

(){}[]/\+*$>.|^?

The below example get three kinds of opening brackets

let specials = "(){}[]"
let pattern = "(\\(|\\{|\\[)"
do {
    let regEx = try NSRegularExpression(pattern: pattern, options: [])
    let nsString = specials as NSString
    let matches = regEx.matches(in: specials, options: [], range: NSMakeRange(0, nsString.length))
    let output = matches.map {nsString.substring(with: $0.range)}
} catch let error as NSError {
    print("Matching failed")
}
//output = ["(", "{", "["]