Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func parseCaptions(text: String) -> String {
- let textRange = NSRange(location: 0, length: text.utf16.count) //NSRange, based on NSString use UTF16 for counting, while Swift.String use UTF8 by default, so `text.count` might be wrong
- let regex = try! NSRegularExpression(pattern: "<text[^>]+>([^<]+)<\\/text>")
- let matches = regex.matches(in: text, range: textRange)
- var result: String = text
- for match in matches.reversed() {
- let wholeNSRange = match.range
- let wholeRange = Range(wholeNSRange, in: result)!
- let textNSRange = match.range(at: 1)
- let textRange = Range(textNSRange, in: result)!
- var string = String(result[textRange])
- string = string.replacingOccurrences(of: "\n", with: " ")
- string = string.replacingOccurrences(of: "'", with: "'")
- string = string.replacingOccurrences(of: "&quot;", with: "\"")
- string.append("\n")
- result = result.replacingCharacters(in: wholeRange, with: string)
- }
- return result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement