import Foundation
let pattern = #"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/([0-9]+))|((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/([0-9]+))"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
<?xml version="1.0" encoding="utf-8"?>
<reg:registerSocResources updateTime="2023-08-23T17:00:00+03:00" formatVersion="1.0" xmlns:reg="http://rkn.gov.ru/register/socResources" xmlns:tns="http://rkn.gov.ru/register/socResources">
<content id="1" hash="c74d571742a6c706828b803d5190e566" includeTime="2022-01-26T22:00:00+03:00">
<resourceName><![CDATA[Единый портал государственных и муниципальных услуг (Госуслуги)]]></resourceName>
<domain><![CDATA[gosuslugi.ru]]></domain>
<ipSubnet>37.220.160.0/21</ipSubnet>
<ipSubnet>46.235.184.0/21</ipSubnet>
<ipSubnet>93.93.88.0/21</ipSubnet>
<ipSubnet>78.41.104.0/21</ipSubnet>
<ipSubnet>185.17.168.0/22</ipSubnet>
<ipSubnet>212.193.144.0/20</ipSubnet>
<ipSubnet>195.208.30.60/30</ipSubnet>
<ipSubnet>195.209.64.0/21</ipSubnet>
<ipSubnet>195.209.80.0/20</ipSubnet>
<ipSubnet>195.208.64.0/20</ipSubnet>
<ipSubnet>195.209.75.0/25</ipSubnet>
<ipSubnet>185.38.204.18/32</ipSubnet>
<ipSubnet>109.207.0.0/20</ipSubnet>
<ipSubnet>213.59.192.0/18</ipSubnet>
</content>
<content id="2" hash="45fcc3ee99b66673c1de92809d7ea403" includeTime="2022-01-26T22:00:00+03:00">
<resourceName><![CDATA[Социальная сеть "ВКонтакте"]]></resourceName>
<domain><![CDATA[vk.com]]></domain>
<ipSubnet>5.181.61.0/24</ipSubnet>
<ipSubnet>79.137.139.0/24</ipSubnet>
<ipSubnet>79.137.164.0/24</ipSubnet>
<ipSubnet>79.137.180.0/24</ipSubnet>
<ipSubnet>79.137.183.0/24</ipSubnet>
<ipSubnet>87.240.128.0/18</ipSubnet>
<ipSubnet>93.186.224.0/20</ipSubnet>
<ipSubnet>95.142.192.0/20</ipSubnet>
<ipSubnet>95.213.0.0/17</ipSubnet>
<ipSubnet>185.32.248.0/22</ipSubnet>
<ipSubnet>185.226.52.0/22</ipSubnet>
<ipSubnet>45.136.20.0/22</ipSubnet>
<ipSubnet>95.163.39.80/29</ipSubnet>
<ipSubnet>193.203.40.0/22</ipSubnet>
<ipSubnet>5.61.20.180/31</ipSubnet>
<ipSubnet>5.61.20.196/30</ipSubnet>
<ipSubnet>5.61.20.248/29</ipSubnet>
<ipSubnet>5.61.23.186/31</ipSubnet>
<ipSubnet>5.61.23.228/31</ipSubnet>
<ipSubnet>5.61.23.248/30</ipSubnet>
<ipSubnet>185.16.247.226/31</ipSubnet>
<ipSubnet>185.100.104.4/30</ipSubnet>
<ipSubnet>185.100.104.44/30</ipSubnet>
<ipSubnet>185.100.104.48/29</ipSubnet>
<ipSubnet>185.100.104.132/30</ipSubnet>
<ipSubnet>185.100.105.24/29</ipSubnet>
<ipSubnet>185.100.105.64/30</ipSubnet>
<ipSubnet>185.100.105.132/30</ipSubnet>
<ipSubnet>185.100.105.192/29</ipSubnet>
<ipSubnet>185.100.105.200/30</ipSubnet>
<ipSubnet>217.20.158.34/31</ipSubnet>
<ipSubnet>5.101.40.2/32</ipSubnet>
<ipSubnet>217.20.152.213/32</ipSubnet>
<ipSubnet>217.20.156.158/32</ipSubnet>
<ipSubnet>217.20.156.18/32</ipSubnet>
<ipSubnet>217.20.155.24/32</ipSubnet>
<ipSubnet>217.20.147.23/32</ipSubnet>
<ipSubnet>95.163.56.192/27</ipSubnet>
<ipSubnet>95.163.41.200/30</ipSubnet>
<ipSubnet>5.61.16.0/21</ipSubnet>
<ipSubnet>185.16.148.0/22</ipSubnet>
<ipSubnet>185.16.244.0/22</ipSubnet>
<ipSubnet>217.20.144.0/20</ipSubnet>
<ipSubnet>87.240.160.0/26</ipSubnet>
<ipSubnet>95.142.201.192/27</ipSubnet>
<ipSubnet>95.142.202.0/27</ipSubnet>
<ipSubnet>95.142.203.192/27</ipSubnet>
<ipSubnet>95.142.206.192/27</ipSubnet>
<ipSubnet>185.32.249.192/27</ipSubnet>
<ipSubnet>185.32.250.192/27</ipSubnet>
<ipSubnet>185.32.251.192/27</ipSubnet>
<ipSubnet>185.100.104.0/22</ipSubnet>
</content>
<ipv6Subnet>2001:0db8:11a3:09d7::/64</ipv6Subnet>
"""#
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