import Foundation
// WARNING: You included a flag that Swift doesn't support: u
// When this flag is set, it makes the pattern and subject strings to be treated as unicode.
// Swift already treats the pattern and subject strings as unicode by default, so including this flag is redundant.
let pattern = #"\d"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
0: 0,Ù ,Û°,߀,०,০,੦,૦,à¦,௦,౦,೦,൦,à¹,à»,༠,á€,á‚,០,á ,᥆,á§,á,á®°,á±€,á±,ê˜ ,ê£,꤀,ê©,ï¼
1: 1,Ù¡,Û±,ß,१,à§§,à©§,à«§,à§,௧,à±§,à³§,൧,๑,໑,༡,á,á‚‘,១,á ‘,᥇,á§‘,á‘,á®±,á±,᱑,꘡,꣑,ê¤,ê©‘,1
2: 2,Ù¢,Û²,ß‚,२,২,੨,૨,à¨,௨,౨,೨,൨,๒,à»’,༢,á‚,á‚’,២,á ’,᥈,á§’,á’,᮲,᱂,á±’,꘢,꣒,꤂,ê©’,ï¼’
3: 3,Ù£,Û³,߃,३,à§©,à©©,à«©,à©,௩,౩,೩,൩,๓,໓,༣,áƒ,á‚“,៣,á “,᥉,á§“,á“,᮳,᱃,᱓,꘣,꣓,꤃,ê©“,3
4: 4,Ù¤,Û´,ß„,४,৪,੪,૪,àª,௪,౪,೪,൪,๔,à»”,༤,á„,á‚”,៤,á ”,᥊,á§”,á”,á®´,᱄,á±”,꘤,꣔,꤄,ê©”,ï¼”
5: 5,Ù¥,Ûµ,ß…,५,à§«,à©«,à««,à«,௫,౫,೫,൫,๕,໕,༥,á…,á‚•,៥,á •,᥋,á§•,á•,᮵,á±…,᱕,꘥,꣕,꤅,ê©•,5
6: 6,Ù¦,Û¶,߆,६,৬,੬,૬,à¬,௬,౬,೬,൬,๖,à»–,༦,á†,á‚–,៦,á –,᥌,á§–,á–,á®¶,᱆,á±–,꘦,꣖,꤆,ê©–,ï¼–
7: 7,Ù§,Û·,߇,à¥,à§,à©,à«,à,à¯,à±,à³,àµ,๗,à»—,༧,á‡,á‚—,៧,á —,á¥,á§—,á—,á®·,᱇,á±—,꘧,꣗,꤇,ê©—,ï¼—
8: 8,Ù¨,Û¸,߈,८,à§®,à©®,à«®,à®,௮,à±®,à³®,൮,๘,໘,༨,áˆ,႘,៨,á ˜,᥎,᧘,á˜,᮸,᱈,᱘,꘨,꣘,꤈,꩘,8
9: 9,Ù©,Û¹,߉,९,৯,੯,૯,à¯,௯,౯,೯,൯,๙,à»™,༩,á‰,á‚™,៩,á ™,á¥,á§™,á™,᮹,᱉,á±™,꘩,꣙,꤉,ê©™,ï¼™
"""#
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