import Foundation
let pattern = #"<RawData>55014B467FFF0C10BEFF00+<\/RawData>\n\n<PrimaryValue>(\d+.\d+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
<?xml version="1.0" encoding="UTF-8"?>
-<Devices-Detail-Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.embeddeddatasystems.com/schema/owserver">
<PollCount>71</PollCount>
<DevicesConnected>2</DevicesConnected>
<LoopTime>2.048</LoopTime>
<DevicesConnectedChannel1>2</DevicesConnectedChannel1>
<DevicesConnectedChannel2>0</DevicesConnectedChannel2>
<DevicesConnectedChannel3>0</DevicesConnectedChannel3>
<DataErrorsChannel1>0</DataErrorsChannel1>
<DataErrorsChannel2>0</DataErrorsChannel2>
<DataErrorsChannel3>0</DataErrorsChannel3>
<VoltageChannel1>4.81</VoltageChannel1>
<VoltageChannel2>4.81</VoltageChannel2>
<VoltageChannel3>4.80</VoltageChannel3>
<VoltagePower>5.05</VoltagePower>
<DeviceName>OWServer_v2-Enet</DeviceName>
<HostName>EDSOWSERVER2</HostName>
<MACAddress>00:04:A3:BE:A9:F2</MACAddress>
<DateTime>2017-03-10 23:18:40</DateTime>
-<owd_DS18B20 Description="Programmable resolution thermometer">
<Name>DS18B20</Name>
<Family>28</Family>
<ROMId>690516B3FC14FF28</ROMId>
<Health>7</Health>
<Channel>1</Channel>
<RawData>54014B467FFF0C10FDFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>21.2500 Deg C</PrimaryValue>
<Temperature Units="Centigrade">21.2500</Temperature>
<UserByte1 Writable="True">75</UserByte1>
<UserByte2 Writable="True">70</UserByte2>
<Resolution>12</Resolution>
<PowerSource>255</PowerSource>
</owd_DS18B20>
-<owd_DS18B20 Description="Programmable resolution thermometer">
<Name>DS18B20</Name>
<Family>28</Family>
<ROMId>020316B4A6C3FF28</ROMId>
<Health>7</Health>
<Channel>1</Channel>
<RawData>55014B467FFF0C10BEFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</RawData>
<PrimaryValue>21.3125 Deg C</PrimaryValue>
<Temperature Units="Centigrade">21.3125</Temperature>
<UserByte1 Writable="True">75</UserByte1>
<UserByte2 Writable="True">70</UserByte2>
<Resolution>12</Resolution>
<PowerSource>255</PowerSource>
</owd_DS18B20>
</Devices-Detail-Response>
"""#
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