利用正则找出[哈哈]emoji

2016-11-30  本文已影响26人  coderST
 attributedTextWithText(text : String)->NSMutableAttributedString?{
        // 表情正则
        let pattern = "\\[.*?\\]"
        guard let regularExpression = try? NSRegularExpression(pattern: pattern, options: []) else { return nil }
        let textCheckingResults = regularExpression.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count))
        if textCheckingResults.count == 0 {
            
            return nil
        }
        
        for result in textCheckingResults{
            let te = (text as NSString).substringWithRange(result.range)
            print(te)
        }
        
        return nil
    }

调用

override func viewDidLoad() {
        super.viewDidLoad()
        
        let emoji = EmojiRegular()
        
        emoji.attributedTextWithText("[哈哈]2123[嘻嘻]")
    }
上一篇 下一篇

猜你喜欢

热点阅读