regex to get string between two % characters

后端 未结 2 1547
感情败类
感情败类 2020-12-10 08:50

I need to extract string between two \"%\" characters, multiple occurrences can be present in the query string. now am using the following regex, can somebody help to get th

2条回答
  •  佛祖请我去吃肉
    2020-12-10 09:19

    I have written a method for regular express. Your regex is fine. You can test your regexes here . The method is:

     func regexInText(regex: String!, text: String!) -> [String] {
    
            do {
                let regex = try NSRegularExpression(pattern: regex, options: [])
                let nsString = text as NSString
                let results = regex.matchesInString(text,
                    options: [], range: NSMakeRange(0, nsString.length))
                return results.map { nsString.substringWithRange($0.range)}
            } catch let error as NSError {
                print("invalid regex: \(error.localizedDescription)")
                return []
            }
        }
    

    You can call it whereever you want.

提交回复
热议问题