正则表达式 – 在Swift中的括号中查找文本
发布时间:2020-12-14 06:31:40 所属栏目:百科 来源:网络整理
导读:如何在Swift中的括号之间获取文本的字符串值数组? 例如:MyFileName(2015)(Type)(createdBy).zip 我想:[2015,Type,createdBy] 只需更新选择的Swift 3答案: func matchesForRegexInText(regex: String!,text: String!) - [String] {do { let regex = try N
如何在Swift中的括号之间获取文本的字符串值数组?
例如:MyFileName(2015)(Type)(createdBy).zip 我想:[2015,Type,createdBy]
只需更新选择的Swift 3答案:
func matchesForRegexInText(regex: String!,text: String!) -> [String] { do { let regex = try NSRegularExpression(pattern: regex,options: []) let nsString = text as NSString let results = regex.matches(in: text,options: [],range: NSMakeRange(0,nsString.length)) return results.map { nsString.substring(with: $0.range)} } catch let error as NSError { print("invalid regex: (error.localizedDescription)") return [] }} 用法保持不变. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |