Advertisement
Larme

Untitled

Jul 28th, 2022
1,598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.94 KB | None | 0 0
  1.     func parseCaptions(text: String) -> String {
  2.         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
  3.         let regex = try! NSRegularExpression(pattern: "<text[^>]+>([^<]+)<\\/text>")
  4.         let matches = regex.matches(in: text, range: textRange)
  5.  
  6.         var result: String = ""
  7.         for match in matches.reversed() {
  8.             let textNSRange = match.range(at: 1)
  9.             let textRange = Range(textNSRange, in: text)!
  10.             var string = String(text[textRange])
  11.             string = string.replacingOccurrences(of: "\n", with: " ")
  12.             string = string.replacingOccurrences(of: "&#39;", with: "'")
  13.             string = string.replacingOccurrences(of: "&amp;quot;", with: "\"")
  14.             string.append("\n")
  15.             result.append(string)
  16.         }
  17.         return result
  18.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement