import Foundation
let pattern = #"intensity\"\s*:\s*\(\"\w+\"\s*:\s*(?<intensity>\d+)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
("data": ["from" :
"2024-04-25T11: 30Z",
"to": "2024-04-2512:00Z",
"intensity": ("forecast": 152,
"actual": null, "index": "moderate"}), ("from": "2024-04-25T12:002",
"intensity": {"forecast": 152, "actual": null, "index": "moderate"}), ("from": "2024-04-25T12:30Z",
"to":
{"from": "2024-04-25T13:00Z", "to":
"2024-04-25T12: 30Z",
("forecast": 164,
"actual": null,
"2024-04-2513: 30Z", "intensity": ("forecast": 154,
"to": "2024-04-25T13: 002",
"intensity": ("forecast": 154,
"actual": null,
"index":
"actual": null, "index": "moderate"}), ("from": "2024-04-25T13:30Z*, "to": "2024-04-25T14:002",
"moderate"}},
"intensity":
04-25T14: 30Z",
"to" :
"index": "moderate"3}, ("from": "2024-04-25T14:002*, "to": "2024-04-25T14:30Z", "intensity": ("forecast": 166, "actual": null, "index": "moderate"}), ("from":
" 2024-04-25T15:00Z"
"actual": nu11,
"index"
"intensity": {"forecast": 170, "actual": null, "index": "moderate"}), {"from": "2024-04-2515: 00Z",
2024-
"to" :
"moderate"}), {"from": "2024-04-25T15:30Z", "to": "2024-04-25T16:00Z", "intensity": ("forecast": 175,
"to": "2024-04-25T15: 30Z",
"intensity": {"forecast": 172,
"2024-04-25T16: 30Z",
"index": "moderate"}}, ("from": "2024-04-25T17:00Z", "to":
"intensity": ("forecast": 177, "actual": null, "index": "moderate"?), ("from": "2024-04-2516: 302",
"actual" : nu11,
"index"
"moderate"}}, ("from": "2024-04-2516: 00Z",
"to": "2024-04-25T17:002",
"intensity": ("forecast": 179,
"actual": null,
2024-04-2517: 30Z", "intensity": ("forecast": 181, "actual": null,
25T18:00Z", "intensity": ("forecast": 184,
"index": "moderate"}}, {"from": "2024-04-25T17: 30Z",
"actual": null, "index": "moderate"}), ("from": "2024-04-25T18:002", "to": "2024-04-25T18: 30Z",
"to": "2024-04-
"moderate"}}, ("from": "2024-04-2518: 30Z", "to": "2024-04-25T19:002",
"intensity": ("forecast": 187, "actual": null,
"intensity": ("forecast": 190,
"actua1": nul1,
"index":
"index":
"high"}}, ("from":
"intensity": {"forecast": 193,
"actual": null, "index":
"2024-04-25T19: 00Z", "to":
"2024-04-25T19: 30Z"
"high"}}, ("from": "2024-04-25T19:30Z", "to": "2024-04-25T20:00Z", "intensity":
{"forecast": 194,
"2024-04-2520: 00Z", "to": "2024-04-25T20:30Z", "intensity": {"forecast": 195, "actual": null, "index": "high"3}, ("from": "2024-04-25T20:30Z",
"actual": null, "index": "high")}, ("from":
"2024-04-25T21:00Z", "intensity": ("forecast":
198, "actual": null, "index": "high"'), ("from": "2024-04-25T21: 002",
"2024-04-25T22: 00Z", "intensity": {"forecast": 187, "actual": null,
"to": "2024-04-25T21: 30Z", "intensity": {"forecast": 196,
'actual": null,
"index": "high"}}, {"from": "2024-04-25T21:302"
"to"
"index": "moderate"}}, ("from": "2024-04-25T22:00Z", "to":
"2024-04-25T22: 30Z",
"intensity": ("forecast": 181, "actual": null,
"index": "moderate"}}, {"from": "2024-04-25T22:30Z", "to": "2024-04-25T23:002", "intensity": ("forecast": 180, "actual": null,
"index'
moderate"}},{"from":
25T23:30Z", "intensity": {"forecast": 172, "actual": null, "index": "moderate"}}, {"from": "2024-04-25T23: 30Z",
"2024-04-25T23:002",
"to":
" 2024-04-
"moderate"}}, {"from": "2024-04-26T00:00Z", "to": "2024-04-2600: 30Z", "intensity": ("forecast": 150,
"to": "2024-04-2600: 00Z",
"intensity": ("forecast": 150,
"actual": null,
"index":
"actual": null, "index": "moderate")}, ("from": "2024-04-26T00: 302",
"to": "2024-04-26T01:00Z"
"intensity": {"forecast": 149,
"actual": null,
"index": "moderate"}}, ("from": "2024-04-26T01:002",
"to": "2024-04-26T01:30Z", "intensity": {"forecast": 149,
"actual": null,
"index":
"moderate"}}, ...
"""#
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