import Foundation
let pattern = #"(\*\w+:\*\s+harmless)+"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"VTDomainMalVendors: *Antiy-AVL*: malicious, *Kaspersky:* harmless, *Avira:* harmless, *Bkav:* undetected, *CMC Threat Intelligence:* harmless, *Snort IP sample list:* harmless, *0xSI_f33d:* undetected, *ViriBack:* harmless, *PhishLabs:* undetected, *K7AntiVirus:* harmless, *CINS Army.:* harmless, *Quttera:* harmless, *PrecisionSec:* undetected, *OpenPhish:* harmless, *VX Vault:* harmless, *ArcSight Threat Intelligence:* undetected, *AlienVault:* harmless, *Sophos:* harmless, *Phishtank:* harmless, *Cyan:* undetected, *Spam404:* harmless, *SecureBrain:* harmless, *CRDF:* harmless, *Fortinet:* harmless, *alphaMountain.ai:* , *Lionic:* harmless, *Cyble:* harmless, *Seclookup:* harmless, *Xcitium Verdict Cloud:* harmless, *Google Safebrowsing:* harmless, *SafeToOpen:* undetected, *ADMINUSLabs:* harmless, *ESTsecurity:* harmless, *Juniper Networks:* harmless, *Heimdal Security:* harmless, *AutoShun:* undetected, *Trustwave:* harmless, *AICC (MONITORAPP:* harmless, *CyRadar:* harmless, *Dr.Web:* , *Emsisoft:* harmless, *Abusix:* harmless, *Webroot:* harmless, *securolytics:* harmless, *AlphaSOC:* undetected, *Acronis:* harmless, *Quick Heal:* harmless, *URLQuery:* undetected, *Viettel Threat Intelligence* harmless, *DNS8:* harmless, *benkow.cc* , *EmergingThreats:* harmless, *Chong Lua Dao:* harmless, *Yandex Safebrowsing:* harmless, *Lumu:* undetected, *zvelo:* undetected, *Bfore.Ai PreCrime:* , *BitDefender:* harmless, *Blueliv:* harmless, *Certego:* harmless, *desenmascara.me:* , *ESET:* harmless, *Forcepoint ThreatSeeker:* harmless, *G-Data:* harmless, *GreenSnow:* harmless, *IPsum:* harmless, *Malwared:* harmless, *MalwarePatrol:* harmless, *malwares.com URL checker:* , *Phishing Database:* harmless, *PREBYTES:* harmless, *Scantitan:* harmless, *SCUMWARE.org:* , *SOCRadar:* harmless, *StopForumSpam:* harmless, *Sucuri SiteCheck:* harmless, *ThreatHive:* harmless, *Threatsourcing:* harmless, *URLhaus:* harmless, *ZeroCERT:* harmless, *Cluster25:* undetected, *Criminal IP:* undetected, *CrowdSec:* undetected, *Netcraft:* undetected, *PhishFort:* undetected, *Segasec:* undetected, *VIPRE:* undetected"#
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