I am trying to get substrings from one string. And for that I am applying regex {[^{]*} But its not working in my swift code and giving me an error "invalid regex". Same regex is working with https://regex101.com/r/B8Gwa7/1. I am using following code to apply regex. I need to get substrings between "{" and "}". Can I get same result without using regex or is there anything wrong with my regex or code?.
static func matches(regex: String, text: String) -> Bool {
do {
let regex = try NSRegularExpression(pattern: regex, options: [.caseInsensitive])
let nsString = text as NSString
let match = regex.firstMatch(in: text, options: [],
range: NSRange(location: .zero, length: nsString.length))
return match != nil
} catch {
print("invalid regex: \(error.localizedDescription)")
return false
}
}
{[^}]*}Secondly, try escaping characters such as{and}{[^}]*}but still getting same error.#"\{.*?\}"#or#"\{[^}]*\}"#.