I got a string like this ...
var string = "A12345678B119292A88B2222A883849B123"
---> (var string = "A12345678B119292A88B2222A883849B123")
... and like to split the string into specific groups starting with 'A' and ending with 'B' to get as result something like this:
resultArray: "12345678", "88", "883849"
This is what I found so far:
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: text,
range: NSRange(text.startIndex..., in: text))
return results.map {
String(text[Range($0.range, in: text)!])
}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
let string = "A12345678B119292A88B2222A883849B123"
let pattern = "\\.*A.*B" // pattern error
let array = matches(for: pattern, in: string)
print(array)
[?] How is it possible to achieve this result using regex and Swift?