但是我不知道如何在Swift 3中做到这一点。
当您收到来自NSRegularexpression的匹配项时,您将获得一个NSTextCheckingResult。您致电
rangeAt以获取特定的捕获组。
例:
let s = "hey ho ha"let pattern = "(h).*(h).*(h)"// our goal is capture group 3, "h" in "ha"let regex = try! NSRegularexpression(pattern: pattern)let result = regex.matches(in:s, range:NSMakeRange(0, s.utf16.count))let third = result[0].rangeAt(3) // <-- !!third.location // 7third.length // 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)