swift 涓嬭浇鏈嶅姟绔枃浠跺湴鍧�url 杞珿BK缂栫爜

姝e垯宸ュ叿灏佽绫伙紙Regex.swift锛� 瀹為檯鍙敤鍒板尮閰嶉獙璇乵atches鏂规硶

 import Foundation

/// 鍩轰簬NSRegularExpression api 鐨勬鍒欏鐞嗗伐鍏风被
public struct Regex {
    
    private let regularExpression: NSRegularExpression
    
    //浣跨敤姝e垯琛ㄨ揪寮忚繘琛屽垵濮嬪寲
    public init(_ pattern: String, options: Options = []) throws {
        regularExpression = try NSRegularExpression(
            pattern: pattern,
            options: options.toNSRegularExpressionOptions
        )
    }
    
    //姝e垯鍖归厤楠岃瘉锛坱rue琛ㄧず鍖归厤鎴愬姛锛�
    public func matches(_ string: String) -> Bool {
        return firstMatch(in: string) != nil
    }
    
    //鑾峰彇绗竴涓尮閰嶇粨鏋�
    public func firstMatch(in string: String) -> Match? {
        let firstMatch = regularExpression
            .firstMatch(in: string, options: [],
                        range: NSRange(location: 0, length: string.utf16.count))
            .map { Match(result: $0, in: string) }
        return firstMatch
    }
    
    //鑾峰彇鎵�鏈夌殑鍖归厤缁撴灉
    public func matches(in string: String) -> [Match] {
        let matches = regularExpression
            .matches(in: string, options: [],
                     range: NSRange(location: 0, length: string.utf16.count))
            .map { Match(result: $0, in: string) }
        return matches
    }
    
    //姝e垯鏇挎崲
    public func replacingMatches(in input: String, with template: String,
                                 count: Int? = nil) -> String {
        var output = input
        let matches = self.matches(in: input)
        let rangedMatches = Array(matches[0.. = {
            return Range(self.result.range, in: self.baseString)!
        }()
        
        //姝e垯琛ㄨ揪寮忎腑姣忎釜鎹曡幏缁勫尮閰嶇殑瀛楃涓�
        public lazy var captures: [String?] = {
            let captureRanges = stride(from: 0, to: result.numberOfRanges, by: 1)
                .map(result.range)
                .dropFirst()
                .map { [unowned self] in
                    Range($0, in: self.baseString)
            }
            
            return captureRanges.map { [unowned self] captureRange in
                if let captureRange = captureRange {
                    return String(describing: self.baseString[captureRange])
                }
                
                return nil
            }
        }()
        
        private let result: NSTextCheckingResult
        
        private let baseString: String
        
        //鍒濆鍖�
        internal init(result: NSTextCheckingResult, in string: String) {
            precondition(
                result.regularExpression != nil,
                "NSTextCheckingResult蹇呴渶浣跨敤姝e垯琛ㄨ揪寮�"
            )
            
            self.result = result
            self.baseString = string
        }
        
        //杩斿洖涓�涓柊瀛楃涓诧紝鏍规嵁鈥滄ā鏉库�濇浛鎹㈠尮閰嶇殑瀛楃涓层��
        public func string(applyingTemplate template: String) -> String {
            let replacement = result.regularExpression!.replacementString(
                for: result,
                in: baseString,
                offset: 0,
                template: template
            )
            
            return replacement
        }
        
        //钘愯淇℃伅
        public var description: String {
            return "Match<\"\(string)\">"
        }
    }
}

鍘熸枃鍑鸿嚜锛�www.hangge.com 杞浇璇蜂繚鐣欏師鏂囬摼鎺ワ細http://www.hangge.com/blog/cache/detail_2170.html

鎵╁睍String

extension String.Encoding {
    static let gb_18030_2000 = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))
}

鍒涘缓杞爜宸ュ叿绫� URLTranscoding.swift

class URLTranscoding{
   //杞珿BK鐨刄RLEncoding
   static func URLEncodingGBK(urlStr:String) -> String{
        let pattern = "[^\\u4E00-\\u9FA5锛屻�傦紒銆佲�︹�︺�娿�嬶紙锛夈�愩�戯細锛涒�溾�樷�濃�欙紵锟"
        let regex = try! Regex(pattern)
        var str = ""
        for i in urlStr{
            if regex.matches(String(i)){
                str.append(i)
            }else{
                
                let data = String(i).data(using: .gb_18030_2000)
                let bytes = [UInt8](data!)
                str.append(bytesConvertToHexstring(byte: bytes))
            }
        }
        return str
    }
    //UInt8杞琱ex瀛楃涓�
   static func bytesConvertToHexstring(byte : [UInt8]) -> String {
        var string = ""
        for val in byte {
            string =  string + "%" + String(format: "%02X",val)
        }
        return string
    }
}

璋冪敤锛�

 let str = "http://58.16.67.112:8088/gzhdoa_app/upload/zwWord/f1f4fe22-09e5-4888-a7db-a876331676c0/姝f枃娴嬭瘯銆愯蹇界暐銆�.doc"
        print(URLTranscoding.URLEncodingGBK(urlStr: str))

杈撳嚭锛�

http://58.16.67.112:8088/gzhdoa_app/upload/zwWord/f1f4fe22-09e5-4888-a7db-a876331676c0/%D5%FD%CE%C4%B2%E2%CA%D4%A1%BE%C7%EB%BA%F6%C2%D4%A1%BF.doc

杞爜瀹屾垚馃ぉ

你可能感兴趣的:(swift 涓嬭浇鏈嶅姟绔枃浠跺湴鍧�url 杞珿BK缂栫爜)