import Foundation
let pattern = #"(?<!\d)(?:\d{12}|\d{8})\.?(?:[0-9xX])(?!\d)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
申请号
002623110
022879803
2004100184779
2004100666004
2005101110245
2006300002925
2006200007177
2007100385983
2008101335952
2008101613271
2011100997897
2008201400425
2009101423229
2009101423233
2009101423248
2009101439477
2009101572384
2009101572399
2009101576224
2009302031381
2009101610776
2009201695623
2011200054060
2011200115844
2011200117483
2011200119760
2011200173252
2009202675059
2009202695692
2010101657760
2010101870386
2010102394457
2010102394599
2010202748438
2010202748442
2010202748461
2010305347887
2010305348019
201120005687X
2011200119900
2011200173290
2010105277084
2010105566205
2011200349620
2011100914818
2011100914945
2011101671599
2011202170507
2011303345684
2011103392345
2011204598533
2011204598548
201120459882X
2011204728081
2011204728185
2011103986574
201220034593X
2012200534787
2012101185754
2012201875401
2012101522194
2012101616153
201210181171X
201210182746X
2012302578183
2012102717768
2012204355254
2012103245533
201210339474X
2012103530528
2012103625443
2012103785724
2012205133542
2012306400917
2013100109283
2013100347741
2013200572675
2013100686967
2013100887627
201310101959X
2013204392703
2013103116132
2013204821094
2013103430914
2013205073300
201320567066X
2013205672542
2013104198233
2013205697728
201310438266X
2013206047657
2013206784136
2013106979478
2013107401728
2013208786475
2014100017892
2014203613224
2014103129866
2014103129917
2014203668496
2014103211663
2014103512302
2014104555767
2014205304935
2014104903341
2014104919602
2014205488892
201420580619X
2014106419682
2014206836544
2014107834019
2014305467389
2015203286585
2015203324750
201510275816X
2015103420381
2015103500672
2015103696669
2015204541209
2015204664745
2015103923075
201510467622X
2015205745010
2015205769284
2015207387890
2015207443706
2015106236775
201510624713X
2015207542082
2015207551452
2015207556085
2015110314940
2015211349209
2015211360104
2015211381702
2016100727763
201610092328X
2016201269151
2016100951186
201620131265X
2016101122274
2016201537615
2016202566259
2016102311131
2016205198507
2016105865929
2016207787667
2016207928858
2016207951375
2016208270639
2016208319490
2016106880470
2016106880555
2016209033823
201621066422X
2016108656802
2016210939684
2016305258880
2016305258895
2017100585076
2017100586609
201720188818X
2017101518115
2017202603036
2017204977946
2017205042156
2017103350053
2017205395444
2017206736253
2017302335447
2017104425076
2017106763096
201721030029X
2017210381102
2017108045288
2017108474438
2017212101341
2017108741867
2017108759339
2017212638027
201710920118X
201710937864X
201710938025X
2017212918579
2017212918668
2017212919919
2017212920244
2018305555227
2017109935766
201710994526X
2017109953730
201710995375X
2017213675894
2017213676191
2017213684060
2017213691670
2017214075375
2017215966451
201730622561X
2017306229748
2017306232613
2017306232628
2017217386944
2018201916983
2018201917914
2018202592290
201810191260X
2018203227754
2018104322962
2018206790888
2018104373610
2018206885731
2018207863744
201820786380X
2018207884967
2018105251184
201820872835X
2018209012008
2018106635891
2018209871622
2018303397159
2018303401597
2018108107645
2018211712732
2018108434473
2018212145039
201810884066X
2018212612587
2018109232869
2018213117869
2018213118293
2018213129669
2018109730845
2018213745478
2018111137608
2018111199286
2018215601739
2018216540022
2018217340269
2018305943345
2018114173714
201821980006X
201811454311X
201822010483X
2018114822527
201822097698X
2018220978171
2019100186094
201920036562X
2019200056202
2019204835323
2019103446604
2019205950070
2019102993026
2019205043321
2019102887031
2019204926623
2019204495410
2019209514723
2019210406537
2019301669399
2019301669257
2019105752159
2019210048170
2019106651386
2019212083094
"""#
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