import Foundation
let pattern = #"[^[:print:]]\[[^a-zA-Z]*[a-zA-Z]"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
[BEGIN TYPESCRIPT]
[H[JLinux dcim-mao 4.19.0-20-cloud-amd64 #1 SMP Debian 4.19.235-1 (2022-03-17) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Jul 10 08:00:55 2023 from 172.17.0.2
[?2004hroot@dcim-mao:~# cd
[?2004l
[?2004hroot@dcim-mao:~# ls
[?2004l
[?2004hroot@dcim-mao:~# cd /
[?2004l
[?2004hroot@dcim-mao:/# ls
[?2004l
app etc media public srv usr
bin home mnt root supervisor.conf var
boot lib opt run sys
dev lib64 proc sbin tmp
[?2004hroot@dcim-mao:/#
[END TYPESCRIPT]
"""##
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