import Foundation
let pattern = #"([^\n\r:]*)(:[ \t])[ \t]*([^\n\r]*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
NetRange: 2001:4860:: - 2001:4860:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
CIDR: 2001:4860::/32
NetName: GOOGLE-IPV6
NetHandle: NET6-2001-4860-1
Parent: ARIN-004 (NET6-2001-4800-0)
NetType: Direct Allocation
OriginAS: AS15169
Organization: Google LLC (GOGL)
RegDate: 2005-03-14
Updated: 2012-02-24
Ref: https://rdap.arin.net/registry/ip/2001:4860::
OrgName: Google LLC
OrgId: GOGL
Address: 1600 Amphitheatre Parkway
City: Mountain View
StateProv: CA
PostalCode: 94043
Country: US
RegDate: 2000-03-30
Updated: 2019-10-31
Comment: Please note that the recommended way to file abuse complaints are located in the following links.
Comment:
Comment: To report abuse and illegal activity: https://www.google.com/contact/
Comment:
Comment: For legal requests: http://support.google.com/legal
Comment:
Comment: Regards,
Comment: The Google Team
Ref: https://rdap.arin.net/registry/entity/GOGL
OrgTechHandle: ZG39-ARIN
OrgTechName: Google LLC
OrgTechPhone: +1-650-253-0000
OrgTechEmail: arin-contact@google.com
OrgTechRef: https://rdap.arin.net/registry/entity/ZG39-ARIN
OrgAbuseHandle: ABUSE5250-ARIN
OrgAbuseName: Abuse
OrgAbusePhone: +1-650-253-0000
OrgAbuseEmail: network-abuse@google.com
OrgAbuseRef: https://rdap.arin.net/registry/entity/ABUSE5250-ARIN
RNOCHandle: ZG39-ARIN
RNOCName: Google LLC
RNOCPhone: +1-650-253-0000
RNOCEmail: arin-contact@google.com
RNOCRef: https://rdap.arin.net/registry/entity/ZG39-ARIN
RTechHandle: ZG39-ARIN
RTechName: Google LLC
RTechPhone: +1-650-253-0000
RTechEmail: arin-contact@google.com
RTechRef: https://rdap.arin.net/registry/entity/ZG39-ARIN
RAbuseHandle: ZG39-ARIN
RAbuseName: Google LLC
RAbusePhone: +1-650-253-0000
RAbuseEmail: arin-contact@google.com
RAbuseRef: https://rdap.arin.net/registry/entity/ZG39-ARIN
"""#
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