import Foundation
let pattern = #"\\VariantSimple=((?:\([^\)]+\))*) \\Processed=((?:\([^\)]+\))*) ([\s\S]*?)(?:>|$)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
>nxp:NX_A0A0A6YYD4-1 \PName=T cell receptor beta variable 13 isoform Iso 1 \GName=TRBV13 \NcbiTaxId=9606 \TaxName=Homo Sapiens \Length=124 \SV=5 \EV=31 \PE=3 \ModResPsi=(52|MOD:00798|half cystine)(120|MOD:00798|half cystine) \ModRes=(106||N-linked (GlcNAc...) asparagine) \VariantSimple=(18|H)(27|V) \Processed=(1|31|PEFF:0001021|signal peptide)(32|124|PEFF:0001020|mature protein) MLSPDLPDSAWNTRLLCRVMLCLLGAGSVAAGVIQSPRHLIKEKRETATLKCYPIPRHDT VYWYQQGPGQDPQFLISFYEKMQSDKGSIPDRFSAQQFSDYHSELNMSSLELGDSALYFC ASSL
>nxp:NX_A0A1B0GV90-1 \PName=Cortexin domain containing 2 isoform Iso 1 \GName=CTXND2 \NcbiTaxId=9606 \TaxName=Homo Sapiens \Length=55 \SV=1 \EV=11 \PE=3 \VariantSimple=(13|N)(22|F)(29|T)(34|Q)(45|T) \Processed=(1|55|PEFF:0001020|mature protein) MEDSSLSSGVDVDKGFAIAFVVLLFLFLIVMIFRCAKLVKNPYKASSTTTEPSLS
"""#
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