import Foundation
let pattern = ##"(?<="Core #3 VID",)\n "SensorValue": "[0-9]+(\,[0-9]+)?"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
[
{
"SensorApp": "HWiNFO",
"SensorClass": "System",
"SensorName": "Virtual Memory Commited",
"SensorValue": "31385",
"SensorUnit": "MB",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "System",
"SensorName": "Virtual Memory Available",
"SensorValue": "5951",
"SensorUnit": "MB",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "System",
"SensorName": "Virtual Memory Load",
"SensorValue": "84",
"SensorUnit": "%",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "System",
"SensorName": "Physical Memory Used",
"SensorValue": "30134",
"SensorUnit": "MB",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "System",
"SensorName": "Physical Memory Available",
"SensorValue": "2338",
"SensorUnit": "MB",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "System",
"SensorName": "Physical Memory Load",
"SensorValue": "92,7",
"SensorUnit": "%",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "System",
"SensorName": "Page File Usage",
"SensorValue": "27,2209569027549",
"SensorUnit": "%",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "CPU [#0]: Intel Xeon E3-1225 v3",
"SensorName": "Core #0 VID",
"SensorValue": "0,7376708984375",
"SensorUnit": "V",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "CPU [#0]: Intel Xeon E3-1225 v3",
"SensorName": "Core #1 VID",
"SensorValue": "0,73828125",
"SensorUnit": "V",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "CPU [#0]: Intel Xeon E3-1225 v3",
"SensorName": "Core #2 VID",
"SensorValue": "0,73828125",
"SensorUnit": "V",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "CPU [#0]: Intel Xeon E3-1225 v3",
"SensorName": "Core #3 VID",
"SensorValue": "0,7449951171875",
"SensorUnit": "V",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "CPU [#0]: Intel Xeon E3-1225 v3",
"SensorName": "Core #0 Clock",
"SensorValue": "983,934044334975",
"SensorUnit": "MHz",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "CPU [#0]: Intel Xeon E3-1225 v3",
"SensorName": "Core #1 Clock",
"SensorValue": "983,934044334975",
"SensorUnit": "MHz",
"SensorUpdateTime": 1558534280
},
{
"SensorApp": "HWiNFO",
"SensorClass": "CPU [#0]: Intel Xeon E3-1225 v3",
"SensorName": "Core #2 Clock",
"SensorValue": "983,934044334975",
"SensorUnit": "MHz",
"SensorUpdateTime": 1558534280
},
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
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