joaoreichert

Mentions Elephant

Dec 1st, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.08 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4.  
  5. var result = "Olá {{51:Daniel S. Debastiani}} como vai? {{1:João Reichert}}"
  6. var patt = "\\{{2}?([\\d]+):([\\s\\S][^\\{]+)\\}{2}"
  7. var dictNames = [String:String]()
  8.  
  9. func matchesForRegexInText(regex: String!, text: String!, dictNameId: [String:String]) -> (fullString:String, dictId:[String:String]){
  10.    
  11.     var stringFormated = text
  12.     var dict = dictNameId
  13.     let regex = NSRegularExpression(pattern: regex, options: nil, error: nil)!
  14.     let nsString = stringFormated as NSString
  15.     let results = regex.matchesInString(stringFormated,options: nil, range: NSMakeRange(0, count(text.utf16))) as! [NSTextCheckingResult]
  16.    
  17.     for match in results as [NSTextCheckingResult] {
  18.         // range at index 0: full match
  19.         // range at index 1: first capture group
  20.         // range at index 2: second capture group
  21.        
  22.         let results2 = regex.matchesInString(stringFormated, options: nil, range: NSMakeRange(0, count(stringFormated.utf16))) as! [NSTextCheckingResult]
  23.        
  24.         let match1 = results2.first!
  25.         println()
  26.        
  27.         let substring0 = (stringFormated as NSString).substringWithRange(match1.rangeAtIndex(0))
  28.         println(substring0)
  29.         let substring1 = (stringFormated as NSString).substringWithRange(match1.rangeAtIndex(1))
  30.         println(substring1)
  31.         let substring2 = (stringFormated as NSString).substringWithRange(match1.rangeAtIndex(2))
  32.         println(substring2)
  33.        
  34.         //dict[substring1] = substring2
  35.        
  36.         println(stringFormated)
  37.        
  38.         println(match1.rangeAtIndex(0))
  39.        
  40.         stringFormated = (stringFormated as NSString).stringByReplacingCharactersInRange(match1.rangeAtIndex(0), withString: substring2)
  41.         println(stringFormated)
  42.         println()
  43.        
  44.     }
  45.    
  46.     return (stringFormated, dict)
  47.    
  48.    
  49. }
  50.  
  51. var format = matchesForRegexInText(patt, result, dictNames)
  52.  
  53. dictNames = format.dictId
  54. var string = format.fullString
  55. //println(dictNames)
  56. //println()
  57. //println(string)
Add Comment
Please, Sign In to add comment