Swift51.com
Swift 头像
Swift  2016-07-03 22:54

Swift 正则表达式类库 Regex

回复:0  查看:7003  感兴趣:24  赞:0  

Regex 是一个 Swift 正则表达式类库。

示例代码:

//创建
// Use `Regex.init(_:)` to build a regex from a static pattern

let greeting = Regex("hello (world|universe)")

// Use `Regex.init(string:)` to construct a regex from dynamic data, and
// gracefully handle invalid input

var validations: [String: Regex]

for (name, pattern) in config.loadValidations() {
  do {
    validations[name] = try Regex(string: pattern)
  } catch {
    print("error building validation \(name): \(error)")
  }
}


//匹配
if greeting.matches("hello universe!") {
  print("wow, you're friendly!")
}

相关开源代码