STRegex 2.1.1

STRegex 2.1.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2020 年 3 月
SPM支持 SPM

Adam SharpAdam Sharp 维护。



STRegex 2.1.1

正则表达式

与老板一样精准的匹配。

用法

创建

// 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!")
}

模式 匹配

switch someTextFromTheInternet {
case Regex("DROP DATABASE (.+)"):
  // TODO: patch security hole
default:
  break
}

捕获

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

if let subject = greeting.firstMatch(in: "hello swift")?.captures[0] {
  print("ohai \(subject)")
}

查找和替换

"hello world".replacingFirst(matching: "h(ello) (\\w+)", with: "H$1, $2!")
// "Hello, world!"

访问最后一个匹配项

switch text {
case Regex("hello (\\w+)"):
  if let friend = Regex.lastMatch?.captures[0] {
    print("lovely to meet you, \(friend)!")
  }
case Regex("goodbye (\\w+)"):
  if let traitor = Regex.lastMatch?.captures[0] {
    print("so sorry to see you go, \(traitor)!")
  }
default:
  break
}

选项

let totallyUniqueExamples = Regex("^(hello|foo).*$", options: [.ignoreCase, .anchorsMatchLines])
let multilineText = "hello world\ngoodbye world\nFOOBAR\n"
let matchingLines = totallyUniqueExamples.allMatches(in: multilineText).map { $0.matchedString }
// ["hello world", "FOOBAR"]

解码

let json = """
  [
    {
      "name" : "greeting",
      "pattern" : "^(\\\\w+) world!$"
    }
  ]
  """.data(using: .utf8)!

struct Validation: Codable {
  var name: String
  var pattern: Regex
}

let decoder = JSONDecoder()
try decoder.decode(Validation.self, from: json)
// Validation(name: "greeting", pattern: /^(\w+) world!/)

范围

let lyrics = """
  So it's gonna be forever
  Or it's gonna go down in flames
  """

let possibleEndings = Regex("it's gonna (.+)")
    .allMatches(in: lyrics)
    .flatMap { $0.captureRanges[0] }
    .map { lyrics[$0] }

// it's gonna: ["be forever", "go down in flames"]

安装

Regex 支持 Swift 4.2 及以上版本,在所有 Swift 平台上。

Swift 包管理器

在您的 Package.swift 中添加一个依赖项

let package = Package(
  name: "MyPackage",
  dependencies: [
    // other dependencies...
    .package(url: "https://github.com/sharplet/Regex.git", from: "2.1.0"),
  ]
)

Carthage

将此行添加到您的Cartfile中

github "sharplet/Regex" ~> 2.1

CocoaPods

将此行添加到您的Podfile中

pod "STRegex", "~> 2.1"

贡献

查看CONTRIBUTING.md

开发设置

Swift包管理器

构建和运行测试

swift test

# or just

rake test:package

如果您使用的是Mac,可以通过Docker for Mac在Linux上进行测试Docker for Mac。设置好了Docker后,启动Linux shell。

rake docker

然后通过Swift包管理器来运行测试。

Xcode

xcpretty 推荐使用,用于美化测试输出

gem install xcpretty

然后运行测试

# one of
rake test:osx
rake test:ios
rake test:tvos

格式化与代码检查

正则表达式使用SwiftFormat以保持一致的代码格式。

正则表达式使用SwiftLint来验证代码风格。SwiftLint通过Hound CI自动在Pull Request上运行。

在提交Pull Request时运行这些工具并解决问题将会非常感谢!

brew bundle
swiftformat .
swiftlint

许可协议

LICENSE.txt