import Foundation
let pattern = #""temp":(.*?),"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"{"vin":810,"vout":283,"cin":46,"cout":133,"temp":34,"charge":75,"relay":0,"pwr":3753,"vch":303,"vfl":303,"ich":40,"ifl":1,"v_relay_on":260,"v_relay_off":250,"mppt_percent":75,"snd_ena":1,"light_mode":1,"contrOn":1,"bat_lo":240,"bat_hi":297,"ssidPass":"pass","ssid":"GardenNet","iLogin":"login","iPass":"pass","cloudName":"url","cloudKey":"api_key","ApPass":"11111111","relay_mode":0,"mppt_mode":3,"st_mode":0,"refr":10,"modd":18,"eday":71,"etot":7,"sec":32,"min":59,"hour":14,"date":5,"month":1,"year":19}"#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression