Hello!
For a while now, I've been developing this app that uses regex to replace words inside a specifig string. I asked for help earlier on this forum, and I got a pretty good answer. However, I don't think the answer was meant for replacing numerous strings at once, as the code that I was given looked like this:
let regex = try! NSRegularExpression(pattern: "\\b"+"Word"+"\\b", options:[])
str = regex.stringByReplacingMatchesInString(str, options: [], range: NSRange(0..<str.utf16.count), withTemplate: "replaced")This led to me first trying out different methods of solving the problem, and I was pretty much left alone with only one solution, by doing it like so:
let regex = try! NSRegularExpression(pattern: "\\b"+"Word"+"\\b", options:[])
str = regex.stringByReplacingMatchesInString(str, options: [], range: NSRange(0..<str.utf16.count), withTemplate: "replaced")
let regex1 = try! NSRegularExpression(pattern: "\\b"+"Word2"+"\\b", options:[])
str = regex1.stringByReplacingMatchesInString(str, options: [], range: NSRange(0..<str.utf16.count), withTemplate: "replaced2")
let regex2 = try! NSRegularExpression(pattern: "\\b"+"Word3"+"\\b", options:[])
str = regex2.stringByReplacingMatchesInString(str, options: [], range: NSRange(0..<str.utf16.count), withTemplate: "replaced3")
This worked fine in the builder and while testing it. But as you can probably guess, I found out that this code was the reason to why Xcode froze while archiving my app. This was of course when I had literally spent probably around 8 hours in total, with over 1500 regexes of mine. I am just wondering if it's a better way of doing this? I could also mention that I didn't use just plain strings, but I had two arrays of the text to replace, and the replaced text. so instead of "Word" it was "toReplace[0]", and "replaced[0]", "toReplace[1] "and replaced[1]" etc.
Help would be greatly appreciated, as I've struggled with this for almost three weeks now.