I have to split a long string with lyrics to a song into lines and then, for each line, split them into words. I'm going to hold this information in a 2 dimensional array.
I've seen some similar questions and they have been solved using [NSRegularExpression] (https://developer.apple.com/documentation/foundation/nsregularexpression) but I can't seem to find any regular expression that equals "everything except something" which is what I want to split on when splitting a string into words.
More specifically I want to split on Everything except alphanumerics or ' or -. In Java this regular expression is [^\\w'-]+
Below is the string, followed by my Swift code to attempt to achieve this task (I just split on whitespace instead of actually splitting on words with "[^\w'-]+" as I can't figure out how to do it.
1 Is this the real life?
2 Is this just fantasy?
3 Caught in a landslide,
4 No escape from reality.
5
6 Open your eyes,
7 Look up to the skies and see,
8 I'm just a poor boy, I need no sympathy,
9 Because I'm easy come, easy go,
10 Little high, little low,
11 Any way the wind blows doesn't really matter to me, to me.
12
13 Mama, just killed a man,
(etc.)
let lines = s?.components(separatedBy: "\n")
var all_words = [[String]]()
for i in 0..<lines!.count {
let words = lines![i].components(separatedBy: " ")
let new_words = words.filter {$0 != ""}
all_words.append(new_words)
}