特性六、正则表达式

来源:互联网 发布:java编程计算n的阶乘 编辑:程序博客网 时间:2024/06/05 22:29
iOS中的正则表达式 用于 ,单个字符串来描述、匹配一系列符合某个句法规则的字符串。比如比配字符串是不是我们跳转的网址,是不是请求的URL。

在iOS中正则表达式的常用选项是
CaseInsensitive 忽略大小写
DotMatchesLineSeparators . 匹配换行符
匹配方案
. 匹配任意字符
* 匹配 0~任意 多个字符
? 尽可能少的重复

匹配函数
matchesInString
重复匹配多次 pattern
如果匹配成功,生成 NSTextCheckingResult 数组
firstMatchInString
匹配第一个 pattern
如果匹配成功,生成 NSTextCheckingResult

匹配结果

numberOfRanges
匹配的 range 计数
如果匹配成功,是 () 的数量 + 1
rangeAtIndex

0 和 pattern 完全匹配的内容
1 第一个 () 的内容
2…依次类推
可以利用 NSString 的 substringWithRange 取得匹配结果

实例一、正则表达式简单匹配Link Text

let str = "<a href=\"http://app.weibo.com/t/feed/310OQS\" rel=\"nofollow\">唐枫</a>"let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)if let result = regex.firstMatchInString(str, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, str.characters.count)) {    print(result.numberOfRanges)    let r1 = result.rangeAtIndex(1)    let r2 = result.rangeAtIndex(2)    print((str as NSString).substringWithRange(r1))    print((str as NSString).substringWithRange(r2))}

http://app.weibo.com/t/feed/310OQs\
唐枫

案例二、正则表达式匹配表情图片,实现图文混排
表情键盘的实现: https://github.com/tangShunZhi/EmotionTest
1、在表情的模型文字匹配 表情

 ///根据表情字符串返回对应的表情符号    private class func emoticon(string string: String) ->TSZEmoticons?{        //遍历所有的表情包的表情数组        var emoticon: TSZEmoticons? = nil        for package in TSZEmoticonsPackage.packages() {            //从数组中查找表情            emoticon = package.emoticons!.filter {$0.chs == string}.last            if emoticon != nil{                break            }        }        return emoticon    } /// 将string生成为带表情符号的属性字符串    class func emoticonText(string: String , font: UIFont) ->NSAttributedString {        //写出正则表达式匹配表情文字   \\表示可以匹配任意的字母或者数字        let pattern = "\\[.*?\\]"        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)        //开始匹配        let results = regex.matchesInString(string, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, string.characters.count))        //获得匹配的数量        var count = results.count        let strM = NSMutableAttributedString(string: string)        //遍历数组 倒着遍历        while count > 0 {            let result = results[--count]            let range = result.rangeAtIndex(0)            //获取表情字符串            let emString = (string as NSString).substringWithRange(range)            //根据字符串获得表情「」            if let emoticon = TSZEmoticonsPackage.emoticon(string: emString){                //根据表情 创建属性字符串                let attrString = TSZEmoticonsAttachment.imageText(emoticon, font: font)                //替换strM 中对应的位置的文本                strM.replaceCharactersInRange(range, withAttributedString: attrString)            }        }            return strM    }

2、文本属性实现混排

class TSZEmoticonsAttachment: NSTextAttachment { /// 记录表情符号    var chs: String?    //通过表情符号建立一个使用TSZEmoticonsAttachment  建立的属性文本    class func imageText(emoticon: TSZEmoticons , font: UIFont) ->NSAttributedString {        //创建图片属性字符串        let attachment  = TSZEmoticonsAttachment()        //记录表情文字        attachment.chs = emoticon.chs        attachment.image = UIImage(contentsOfFile: emoticon.imagePath)        //设置边界        let h = font.lineHeight        //提示:在 scrollView 中,bounds 的原点 就是 contentOffset        attachment.bounds = CGRect(x: 0, y: -4, width: h, height: h)        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))        //添加文本字体的属性        imageText.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, 1))        return imageText    }}
0 0
原创粉丝点击