import Foundation
let pattern = #"[a-zA-Z]+\s\s\d|(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])|([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})|(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])|0[1-9]|[12][0-9]|3[01][-/.](0[1-9]|1[012])[-/.](19|20)|W(Deny)|(Permit)|(Global)|(Trust)|(Untrust)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:01" duration=0 policy_id=13 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=23.15.10.113 dst=121.120.246.39 src_port=62160 dst_port=443 src-xlated ip=23.15.10.113 port=62160 dst-xlated ip=121.120.246.39 port=443 session_id=504659 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 443 0 10.1.24.91 main 23.15.10.113 1 13 62160 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 504659 udp:514 log_management indexer_kldc5 23.15.10.113 62160 4/5/2017 14:37 16 0 Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-biz02-mbbkulfm2uns02 mbbkulfm2uns02 - - - NetScreen device_id=mbbkulfm2uns02 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:00" duration=0 policy_id=324 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=113.210.200.243 dst=202.162.17.118 src_port=40558 dst_port=443 src-xlated ip=113.210.200.243 port=40558 dst-xlated ip=202.162.17.118 port=443 session_id=297570 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local mbbkulfm2uns02 202.162.17.118 443 0 10.1.24.91 main 113.210.200.243 1 324 40558 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 297570 udp:514 log_management indexer_kldc5 113.210.200.243 40558 4/5/2017 14:37 16 0 Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-myloca-drm2x01 MBBKULFMLCGW01 - - - NetScreen device_id=MBBKULFMLCGW01 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:01" duration=0 policy_id=5 service=https proto=6 src zone=V1-Untrust dst zone=V1-Trust action=Permit sent=0 rcvd=0 src=203.106.85.190 dst=203.153.92.65 src_port=48946 dst_port=443 src-xlated ip=203.106.85.190 port=48946 dst-xlated ip=203.153.92.65 port=443 session_id=249013 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFMLCGW01 203.153.92.65 443 0 10.1.24.91 main 203.106.85.190 1 5 48946 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 249013 udp:514 log_management indexer_kldc5 203.106.85.190 48946 4/5/2017 14:37 16 0 V1-Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:02" duration=0 policy_id=560 service=https proto=6 src zone=V1-Trust dst zone=V1-Untrust action=Permit sent=0 rcvd=0 src=202.162.18.66 dst=52.6.70.163 src_port=39512 dst_port=443 src-xlated ip=202.162.18.66 port=39512 dst-xlated ip=52.6.70.163 port=443 session_id=208829 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 52.6.70.163 443 0 10.1.24.91 main 202.162.18.66 1 560 39512 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 208829 udp:514 log_management indexer_kldc5 202.162.18.66 39512 4/5/2017 14:37 16 0 V1-Trust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:02" duration=0 policy_id=574 service=udp/port:15000 proto=17 src zone=V1-Untrust dst zone=V1-Trust action=Deny sent=0 rcvd=125 src=8.8.8.8 dst=202.162.18.190 src_port=53 dst_port=15000 session_id=0 2017-05-04T14:37:26.000+0800 Deny 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 202.162.18.190 15000 0 10.1.24.91 main 1 574 17 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 125 0 udp/port:15000 0 udp:514 log_management indexer_kldc5 8.8.8.8 53 4/5/2017 14:37 16 0 V1-Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:02" duration=0 policy_id=560 service=dns proto=17 src zone=V1-Trust dst zone=V1-Untrust action=Permit sent=0 rcvd=0 src=202.162.18.130 dst=8.8.8.8 src_port=26311 dst_port=53 src-xlated ip=202.162.18.130 port=26311 dst-xlated ip=8.8.8.8 port=53 session_id=185596 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 8.8.8.8 53 0 10.1.24.91 main 202.162.18.130 1 560 26311 17 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 dns 185596 udp:514 log_management indexer_kldc5 202.162.18.130 26311 4/5/2017 14:37 16 0 V1-Trust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:02" duration=0 policy_id=560 service=https proto=6 src zone=V1-Trust dst zone=V1-Untrust action=Permit sent=0 rcvd=0 src=202.162.18.66 dst=52.6.70.163 src_port=39511 dst_port=443 src-xlated ip=202.162.18.66 port=39511 dst-xlated ip=52.6.70.163 port=443 session_id=200606 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 52.6.70.163 443 0 10.1.24.91 main 202.162.18.66 1 560 39511 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 200606 udp:514 log_management indexer_kldc5 202.162.18.66 39511 4/5/2017 14:37 16 0 V1-Trust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-biz02-mbbkulfm2uns02 mbbkulfm2uns02 - - - NetScreen device_id=mbbkulfm2uns02 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:00" duration=0 policy_id=270 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=202.162.17.210 dst=202.162.17.122 src_port=41814 dst_port=443 src-xlated ip=202.162.17.210 port=41814 dst-xlated ip=202.162.17.122 port=443 session_id=391371 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local mbbkulfm2uns02 202.162.17.122 443 0 10.1.24.91 main 202.162.17.210 1 270 41814 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 391371 udp:514 log_management indexer_kldc5 202.162.17.210 41814 4/5/2017 14:37 16 0 Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:01" duration=0 policy_id=13 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=203.106.85.197 dst=121.120.246.39 src_port=50539 dst_port=443 src-xlated ip=203.106.85.197 port=50539 dst-xlated ip=121.120.246.39 port=443 session_id=504075 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 443 0 10.1.24.91 main 203.106.85.197 1 13 50539 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 504075 udp:514 log_management indexer_kldc5 203.106.85.197 50539 4/5/2017 14:37 16 0 Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-biz02-mbbkulfm2uns02 mbbkulfm2uns02 - - - NetScreen device_id=mbbkulfm2uns02 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:00" duration=0 policy_id=2 service=icmp proto=1 src zone=Global dst zone=Global action=Permit sent=0 rcvd=0 src=202.162.17.210 dst=202.162.17.119 icmp type=8 icmp code=0 src-xlated ip=202.162.17.210 dst-xlated ip=202.162.17.119 session_id=400977 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 0 14 4 37 may 26 thursday 2017 local mbbkulfm2uns02 202.162.17.119 0 10.1.24.91 main 202.162.17.210 1 2 1 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 icmp 400977 udp:514 log_management indexer_kldc5 202.162.17.210 4/5/2017 14:37 16 0 8 Global
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:01" duration=0 policy_id=13 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=203.106.85.20 dst=121.120.246.39 src_port=54543 dst_port=443 src-xlated ip=203.106.85.20 port=54543 dst-xlated ip=121.120.246.39 port=443 session_id=519071 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 443 0 10.1.24.91 main 203.106.85.20 1 13 54543 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 519071 udp:514 log_management indexer_kldc5 203.106.85.20 54543 4/5/2017 14:37 16 0 Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:02" duration=0 policy_id=445 service=dns proto=17 src zone=V1-Trust dst zone=V1-Untrust action=Permit sent=0 rcvd=0 src=202.162.18.191 dst=8.8.4.4 src_port=59640 dst_port=53 src-xlated ip=202.162.18.191 port=59640 dst-xlated ip=8.8.4.4 port=53 session_id=207747 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 8.8.4.4 53 0 10.1.24.91 main 202.162.18.191 1 445 59640 17 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 dns 207747 udp:514 log_management indexer_kldc5 202.162.18.191 59640 4/5/2017 14:37 16 0 V1-Trust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-biz02-mbbkulfm2uns02 mbbkulfm2uns02 - - - NetScreen device_id=mbbkulfm2uns02 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:00" duration=0 policy_id=109 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=14.1.225.89 dst=202.162.17.152 src_port=58279 dst_port=443 src-xlated ip=14.1.225.89 port=58279 dst-xlated ip=202.162.17.152 port=443 session_id=329728 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local mbbkulfm2uns02 202.162.17.152 443 0 10.1.24.91 main 14.1.225.89 1 109 58279 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 329728 udp:514 log_management indexer_kldc5 14.1.225.89 58279 4/5/2017 14:37 16 0 Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:01" duration=0 policy_id=13 service=http proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=23.11.200.142 dst=121.120.246.39 src_port=52451 dst_port=80 src-xlated ip=23.11.200.142 port=52451 dst-xlated ip=121.120.246.39 port=80 session_id=507541 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 80 0 10.1.24.91 main 23.11.200.142 1 13 52451 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 http 507541 udp:514 log_management indexer_kldc5 23.11.200.142 52451 4/5/2017 14:37 16 0 Untrust
May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time="2017-05-04 14:37:01" duration=0 policy_id=13 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=203.106.85.109 dst=121.120.246.39 src_port=36278 dst_port=443 src-xlated ip=203.106.85.109 port=36278 dst-xlated ip=121.120.246.39 port=443 session_id=518451 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 443 0 10.1.24.91 main 203.106.85.109 1 13 36278 6 ___::_...__--::+:_--__-_-_-__=__[]--():_="--_::"_= 0 Creation 0 https 518451 udp:514 log_management indexer_kldc5 203.106.85.109 36278 4/5/2017 14:37 16 0 Untrust
"""#
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