I am looking to extract and print strings using swift on regex but am stuck on how to do it. I am new to swift and regex on swift so having a hard time figuring it out
In the below example I want to print 62AD and 21BC (Whatever is inside the square brackets). I could only come up with this code but this doesnt extract anything and gives a nil value. The newline character and the quotes are part of the string
let output = "\"Living Room\" [62AD]\n Hub \"Living Room\" [21BC]:"
let range = NSRange(output.startIndex..<output.endIndex,in: output)
let patternToMatch1 = "\"Living Room\" [.*]+\n"
let patternToMatch2 = "Hub \"Living Room\" [^:]+:"
let captureRegex1 = try! NSRegularExpression(pattern: patternToMatch1, options: [])
let captureRegex2 = try! NSRegularExpression(pattern: patternToMatch2, options: [])
let matches1 = captureRegex1.matches(in: output,options: [],range: range)
guard let match1 = matches1.first else {
throw NSError(domain: "", code: 0, userInfo: nil)
}
print("\(String(describing:match1))")
let matches2 = captureRegex2.matches(in: output,options: [],range: range)
guard let match2 = matches2.first else {
throw NSError(domain: "", code: 0, userInfo: nil)
}
print("\(String(describing:match2))")