You can use String method enumerateSubstrings in range using byWords options, check if the string is an integer, if so append a new array with that string to the result otherwise append the word to the last result array:
let string = "001|apple|red|002|banana|yellow|003|grapes|purple"
var result: [[Substring]] = []
string.enumerateSubstrings(in: string.startIndex..., options: .byWords) { _, range, _, _ in
let word = string[range]
if let _ = Int(word) {
result.append([word])
// or just a new empty array
// result.append([])
} else {
result[result.index(before: result.endIndex)].append(word)
}
}
print(result) // "[["001", "apple", "red"], ["002", "banana", "yellow"], ["003", "grapes", "purple"]]\n"