import Foundation
let pattern = #"^(Primary|Secondary|Tertiary).Gene:.([A-Z]|[a-z])+ \(1\)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
1([A-Z]|[a-z])+
Scroll of Renaming (1)
Seller: Rogrinus
Expires in: 07 hr, 09 min, 46 sec
Selling price:
200000
1
Secondary Gene: Loop (Veilspun) (1)
Seller: Natta
Expires in: 09 hr, 21 min, 47 sec
Selling price:
200000
1
Primary Gene: Python (1)
Seller: PlatinumStardust
Expires in: 1 days, 07 hr, 37 min, 50 sec
Selling price:
200000
1
Primary Gene: Stitched (Veilspun) (1)
Seller: lizagem
Expires in: 2 days, 11 hr, 07 min, 56 sec
Selling price:
200000
1
Primary Gene: Stitched (Veilspun) (1)
Seller: ScarletCyanide
Expires in: 3 days, 06 hr, 31 min, 26 sec
Selling price:
200000
1
Tertiary Gene: Capsule (Veilspun) (1)
Seller: forcipiger
Expires in: 4 days, 04 hr, 15 min, 26 sec
Selling price:
200000
1
Tertiary Gene: Firefly (Veilspun) (1)
Seller: Pyracantha
Expires in: 4 days, 23 hr, 35 min, 53 sec
Selling price:
200000
1
Primary Gene: Python (1)
Seller: sisyo
Expires in: 5 days, 13 hr, 59 min, 20 sec
Selling price:
200000
1
Tertiary Gene: Squiggle (Banescale) (1)
Seller: BananaMeteor
Expires in: 5 days, 18 hr, 32 min, 31 sec
Selling price:
200000
1
Primary Gene: Skink (1)
Seller: H0ur
Expires in: 6 days, 13 hr, 07 min, 31 sec
Selling price:
200000
"""#
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