美文网首页
Swift 之使用正则表达式

Swift 之使用正则表达式

作者: willokyes | 来源:发表于2018-03-12 15:18 被阅读0次
    
    //
    //  String+Extension.swift
    //  WeiBo
    //
    //  Created by 八月夏木 on 2018/3/12.
    //  Copyright © 2018年 八月夏木. All rights reserved.
    //
    
    import Foundation
    
    extension String {
        // get link and text from href string
        func cz_href() -> (link: String, text: String)? {
            //
            // <a href="http://app.weibo.com/t/feed/6vtZb0" rel="nofollow">微博 weibo.com</a>
            
            let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
           
            guard let regx = try? NSRegularExpression(pattern: pattern, options: []),
                let result = regx.firstMatch(in: self, options: [], range: NSMakeRange(0, count)) else {
                return nil
            }
            
            let link = (self as NSString).substring(with: result.range(at: 1))
            let text = (self as NSString).substring(with: result.range(at: 2))
            
            //
            return (link, text)
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:Swift 之使用正则表达式

          本文链接:https://www.haomeiwen.com/subject/kbinfftx.html