import Foundation
let pattern = #"^[^|]*(?:\|[^|]*){58}$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
10001|W20101|W20101|G00001||||學徒劍盾|8|9|768||-1|1||||||||40002||||||1|14||2||40027|40028|40029|40030||2|22|113|||||||||||2|50|100|7|||||
10002|W30101|W30101|G00001||||學徒大斧|9|9|768||-1|1||||||||40003||||||1|17||3||40031|40032|40033|40034||2|26|142|||||||||||2|50|100|9|||||
10479|I00208||G00005||||青鐵礦|29||0||-1|30||||3|||||||||100|5|1||54|$53$原始的礦石,整體泛著鐵青的色澤。
#IMG$NoticeIcon#30~45級的副本掉落或跟公會商人購買。
$7$能與其他材料結合,製作40級的合金材料。
|||||||||||||||||||||||||||
"""##
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