import Foundation
let pattern = #"""
(?<id>[^\|]*)\|(?<action>[^\|]*)\|(?<date_create>[^\|]*)\|(?<actor_type>[^\|]*)\|(?<actor_user_id>[^\|]*)\|(?<actor_user_name>[^\|]*)\|(?<actor_user_email>[^\|]*)\|(?<entity_type>[^\|]*)\|(?<entity_user_id>[^\|]*)\|(?<entity_user_name>[^\|]*)\|(?<entity_user_email>[^\|]*)\|(?<entity_workspace_id>[^\|]*)\|(?<entity_workspace_name>[^\|]*)\|(?<entity_workspace_domain>[^\|]*)\|(?<entity_enterprise_id>[^\|]*)\|(?<entity_enterprise_name>[^\|]*)\|(?<entity_enterprise_domain>[^\|]*)\|(?<entity_channel_id>[^\|]*)\|(?<entity_channel_name>[^\|]*)\|(?<entity_channel_privacy>[^\|]*)\|(?<entity_channel_is_shared>[^\|]*)\|(?<entity_channel_is_org_shared>[^\|]*)\|(?<entity_app_id>[^\|]*)\|(?<entity_app_name>[^\|]*)\|(?<entity_app_is_distributed>[^\|]*)\|(?<entity_app_is_directory_approved>[^\|]*)\|(?<entity_app_scopes>[^\|]*)\|(?<entity_file_id>[^\|]*)\|(?<entity_file_name>[^\|]*)\|(?<entity_file_filetype>[^\|]*)\|(?<entity_file_title>[^\|]*)\|(?<context_type>[^\|]*)\|(?<context_location_id>[^\|]*)\|(?<context_location_name>[^\|]*)\|(?<context_location_type>[^\|]*)\|(?<context_location_domain>[^\|]*)\|(?<context_ua>[^\|]*)\|(?<context_ipaddress>[^\|]*)\|(?<details_name>[^\|]*)\|(?<details_type>[^\|]*)\|(?<details_previous_value>[^\|]*)\|(?<details_new_value>[^\|]*)\|(?<details_export_type>[^\|]*)\|(?<details_export_start_ts>[^\|]*)\|(?<details_export_end_ts>[^\|]*)\|(?<details_is_internal_integration>[^\|]*)\|(?<details_kicker_id>[^\|]*)\|(?<details_kicker_name>[^\|]*)\|(?<details_kicker_email>[^\|]*)\|(?<details_inviter_id>[^\|]*)\|(?<details_inviter_name>[^\|]*)\|?(?<details_inviter_name2>[^\|]*)\|?(?<details_inviter_email>[\w+]*)
"""#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
f6c75963-192e-4cb7-b25e-aef5ca3dd7de|file_shared|02/05/2019 19:07|user|WC31RPBJ4|Saumya Khanna|Saumya.Khanna@marsh.com|file||||||||||||||||||||FFZ3JH8CV|image.png|image/png|Pasted image at 2019-02-05, 2:07 PM||EB6CVT82J|Marsh & McLennan Companies, Inc.|enterprise|mmc|||||||||||||||||
903dd3e5-6c28-49fa-bc4a-7997daa7abb5|user_channel_join|01/17/2019 20:01|user|WALL8ECLC|Mary Yao|Mary.Yao@oliverwyman.com|channel||||||||||CFGD6P552|studioculture|public|false|false|||||||||||TCX7L0SP5|NY Studio|workspace|ny-studio|Mozilla/5.0 (Windows NT 10.0.14393; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) AtomShell/3.3.3 Chrome/61.0.3163.100 Electron/2.0.7 Safari/537.36 Slack_SSB/3.3.3|167.245.255.229||INVITED||||||||||WA50E2AAX|Dustin Hughes|dustin.hughes@oliverwyman.com
(?<id>[^\|]*)\|(?<action>[^\|]*)\|(?<date_create>[^\|]*)\|(?<actor_type>[^\|]*)\|(?<actor_user_id>[^\|]*)\|(?<actor_user_name>[^\|]*)\|(?<actor_user_email>[^\|]*)\|(?<entity_type>[^\|]*)\|(?<entity_user_id>[^\|]*)\|(?<entity_user_name>[^\|]*)\|(?<entity_user_email>[^\|]*)\|(?<entity_workspace_id>[^\|]*)\|(?<entity_workspace_name>[^\|]*)\|(?<entity_workspace_domain>[^\|]*)\|(?<entity_enterprise_id>[^\|]*)\|(?<entity_enterprise_name>[^\|]*)\|(?<entity_enterprise_domain>[^\|]*)\|(?<entity_channel_id>[^\|]*)\|(?<entity_channel_name>[^\|]*)\|(?<entity_channel_privacy>[^\|]*)\|(?<entity_channel_is_shared>[^\|]*)\|(?<entity_channel_is_org_shared>[^\|]*)\|(?<entity_app_id>[^\|]*)\|(?<entity_app_name>[^\|]*)\|(?<entity_app_is_distributed>[^\|]*)\|(?<entity_app_is_directory_approved>[^\|]*)\|(?<entity_app_scopes>[^\|]*)\|(?<entity_file_id>[^\|]*)\|(?<entity_file_name>[^\|]*)\|(?<entity_file_filetype>[^\|]*)\|(?<entity_file_title>[^\|]*)\|(?<context_type>[^\|]*)\|(?<context_location_id>[^\|]*)\|(?<context_location_name>[^\|]*)\|(?<context_location_type>[^\|]*)\|(?<context_location_domain>[^\|]*)\|(?<context_ua>[^\|]*)\|(?<context_ipaddress>[^\|]*)\|(?<details_name>[^\|]*)\|(?<details_type>[^\|]*)\|(?<details_previous_value>[^\|]*)\|(?<details_new_value>[^\|]*)\|(?<details_export_type>[^\|]*)\|(?<details_export_start_ts>[^\|]*)\|(?<details_export_end_ts>[^\|]*)\|(?<details_is_internal_integration>[^\|]*)\|(?<details_kicker_id>[^\|]*)\|(?<details_kicker_name>[^\|]*)\|(?<details_kicker_email>[^\|]*)\|(?<details_inviter_id>[^\|]*)\|(?<details_inviter_name>[^\|]*)\|(?<details_inviter_email>(\w+))?\|
"""#
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