import Foundation
let pattern = #"^\s+$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
General Information
Brand:
Whirlpool
Model name:
PACW9HP
Type:
On/Off
Construction Type:
Free-Standing
Number of Speeds:
7
Mode
Cooling:
Yes
Heating:
Yes
Antibacterial Filters:
Yes
Ionization:
Yes
Sleep Function:
Yes
Conditioner Features
Recommended Area:
25 - 30 m²
Capacity Cooling (BTU):
9000
Capacity Heating (BTU):
8500
Capacity Cooling (W):
3000
Capacity Heating (W):
2000
Air Flow:
350 m³/h
Indoor Noise Level:
65
HEPA Filter:
Yes
Compressor
Refrigerant:
R410A
Type of Compressor:
Rotary
Energy Rating
Energy Efficiency Class:
A+
Remote Control
Remote Control:
Yes
Timer on / off:
Yes
Power
Power from:
Network
Voltage:
200-240 V
Cord Length:
1.2 m
Color
Color:
White
Dimensions
Dimensions (W x H x D):
448 x 743 x 400 mm
Weight
Weight:
31 kg
Warranty
Warranty:
36 Months
"""#
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