加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 在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 []
}}

用法保持不变.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读