There is a clever 1st answer given here for splitting swift string with a regex expression
split string answer
However it keeps the searched text within the
You can define an extension like this:
extension String {
func split(usingRegex pattern: String) -> [String] {
//### Crashes when you pass invalid `pattern`
let regex = try! NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: self, range: NSRange(0..["hi", "this", "should", "be", "separated"]
The above code does not work as you expect when you use "\\||Z|ZY"
, but I think you can modify your pattern to fit into this extension.