最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[未解决]Swift中字符串的正则表达式处理:判断字符串是否符合某个类型

Swift crifan 3515浏览 0评论

想要把:

let personIdExample = "user-ae96618f-3a5c-48a8-a799-9944a4e76ed9"
let groupIdExample = "group-7f1ad5cd-8f18-4c5e-9fe4-604c3e5b1ab8"
let topicIdExample = "topic-a11838ca-a741-4d2d-ad5c-477c87400ad0"
let teamIdExample = "team-d655d10c-a0dc-46f6-804d-c88866538036"
let contactIdExample = "contact-1be1e474-388e-4b30-9030-40add6348cd4"

去完全匹配,以便于获得对应的:

对应的Id的类型

swift regular expression match

NSTextCheckingResult Class Reference

NSRegularExpression Class Reference

ios – Swift extract regex matches – Stack Overflow

想要提取出:

user-ae96618f-3a5c-48a8-a799-9944a4e76ed9

中的各个子项sub group:

user

ae96618f

3a5c

48a8

a799

9944a4e76ed9

但是却没法活的对应的C#中的那种sub group

即 每个子的匹配项

swift regular expression match sub group

Swift regex – How to extract a matching group – Stack Overflow

regex – How to group search regular expressions using swift – Stack Overflow

Swift regex – How to extract a matching group – www.scriptscoop.net

SwiftRegex/SwiftRegex.swift at master · johnno1962/SwiftRegex · GitHub

Swift regular expressions — Swift Studies

Swift extension for Native String class to support Regex match and Regex replace.  GitHub

Regular Expression – Swift Must Know Tips

Clean Regular Expressions In Swift

NSRegularExpression Tutorial: Getting Started – Ray Wenderlich

NSRegularExpression: How to match regular expressions in strings – Swift 2 example code

ios – Swift regex: does a string match a pattern? – Stack Overflow

目前是,用了下面的代码:

//: Playground - noun: a place where people can play

import UIKit

let personIdExample = "user-ae96618f-3a5c-48a8-a799-9944a4e76ed9"
let groupIdExample = "group-7f1ad5cd-8f18-4c5e-9fe4-604c3e5b1ab8"
let topicIdExample = "topic-a11838ca-a741-4d2d-ad5c-477c87400ad0"
let teamIdExample = "team-d655d10c-a0dc-46f6-804d-c88866538036"
let contactIdExample = "contact-1be1e474-388e-4b30-9030-40add6348cd4"

do {
  var idTypePattern = ""
  idTypePattern = "^[a-zA-Z]{4,7}(-\\w{4,20}){5}$"
  idTypePattern = "^([a-zA-Z]{4,7})-"
  idTypePattern = "^([a-zA-Z]{4,7})(-\\w{4,20}){5}$"
  idTypePattern = "^([a-zA-Z]{4,7})(-\\w{4,20})"
  let idRegExp:NSRegularExpression = try NSRegularExpression(pattern: idTypePattern, options: NSRegularExpressionOptions.CaseInsensitive)
  print("idRegExp=\(idRegExp)")

  let idMatchs:[NSTextCheckingResult] = idRegExp.matchesInString(personIdExample, options: [], range: NSRange(location: 0, length: personIdExample.characters.count))
  print("idMatchs=\(idMatchs)")

  for (idx, curIdMatch) in idMatchs.enumerate() {
    print("[\(idx)] \(curIdMatch)")
  }

  if let idMatchFirst = idMatchs.first {
    print("idMatchFirst=\(idMatchFirst)")

    let idTypeRange = idMatchFirst.range
    print("idTypeRange=\(idTypeRange)")
  }
}catch let error {

}

func matchesForRegexInText(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 []
  }
}

let matches = matchesForRegexInText("idTypePattern", text: personIdExample)

print(matches)

 

还是没有得到想要的结果:

以后有空再去弄吧。。

总之,和Python,C#,甚至Java比,

Swift中对于正则表达式的支持,太你妹的弱智了:

连基本的sub group都不支持。。。

去换用:

[已解决]Swift中去判断字符串是否以某种格式开始

转载请注明:在路上 » [未解决]Swift中字符串的正则表达式处理:判断字符串是否符合某个类型

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.194 seconds, using 22.15MB memory