import Foundation
let pattern = #"(\\\w\\\w)|(\\[A-Za-z0-9]{5}\b)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
@DethDeelinDale DALE BUB⁉️\n\nU ALRIT⁉️\n\nU NEED BAKUP⁉️
RT @DethDeelinDale: WHO N THE FUK R U #DIKS⁉️\n\nHOLEE FUK‼️ PUT DEM \uD83D\uDD2B AWAY!!!\n\nU kan hav the \uD83D\uDCB0 I dun giv a fuk bout dat!!!\n\nHear u go…jus ta…
@DethDeelinDale Waz rong dale⁉️\n\nWaz it a menshall er 1 a em lite \uD83D\uDEAC⁉️\n\nI dun HAYT thoz‼️\n\nNuthin but #MarbReds\uD83D\uDEAC 4 me‼️
WAT N THE FLYIN FUK⁉️\n\nHEY, U DUM B!TCH, PULL UP UR FN \uD83D\uDC56‼️\n\nU KANT NOT FRIGGEN \uD83D\uDCA9 HER‼️\n\nI swar 2 fn God, thez #ratards\uD83E\uDD24 @ friggen @Walmart r gonna dun mayk me looz mi fn mind!!! \uD83E\uDD2C\uD83E\uDD2C https://t.co/xsQg26SWIk
WOOD U HUREE THE FUK UP U STUPID LITLE KIDS N GET ON UR GOD DAM SKOOL \uD83D\uDE8C!!! \uD83E\uDD2C\n\nU MOTHAFUKERS R GONNA DUN MAYK ME LAYT AGAN 2 MI JOB #HAWLINKARTS\uD83D\uDED2 @Walmart‼️\n\nThas it! I aint not got Tim 4 this, Im passin thez fn ratards!!! \uD83D\uDCA8\n\nOhhhh fukkkkk... \uD83D\uDEA8\uD83D\uDE94\uD83E\uDD26\uD83C\uDFFB♂️\uD83E\uDD26\uD83C\uDFFB♂️
WAR N TYE FLYIN FUK M I N WAT HAPND 2 MI FN \uD83D\uDC56!!!???
@DethDeelinDale Wat 4 bub⁉️ U dun need mi #glock \uD83D\uDD2B???
RT @DethDeelinDale: I NEEDA #GAT \uD83D\uDD2B‼️
RT @DethDeelinDale: BOOOOOMMMMM‼️ \n\nDAS WAT I THOT #BALDYBILL \uD83D\uDC68\uD83C\uDFFB\uD83E\uDDB2‼️\n\nU cum talkin all dat SHIT 2 me n think u waznt gonna get put 2 fukin…
Hapins 2 the best a us! \uD83D\uDC56\uD83D\uDCA9 https://t.co/TVzch1v0du
"""##
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