本项目为 Swift String 提供了一个轻量级/简单的正则表达式扩展。
此软件包使用 Swift Package Manager 构建,并且是 Perfect 项目的一部分。 它的编写目的是独立运行,因此不需要 PerfectLib 或任何其他组件。
请确保您已安装并激活最新的 Swift 4.0 工具链。
将此项目作为依赖项添加到您的 Package.swift 文件中。
.package(url:"https://github.com/PerfectSideRepos/Perfect-RegEx.git", from: "3.1.0")
...
dependencies: ["Regex"]
然后请将以下行添加到 Swift 源代码的开头部分
import Regex
以下演示展示了如何使用模式提取子字符串范围
var source = "there is a bloody bad bread on my bed."
let ranges = source.match(pattern: "b[a-z]+d")
// it will figure out the range of `blood`, `bad` `bread` and `bed`
let found:[String] = ranges.map { String(source[$0]) }
print("found", found)
// you can do further operations, such as remove:
source.removeSubrange(ranges[0])
print(source)
// the result should be:
// there is a y bad bread on my bed.
extension String {
/// test if the string contains certain pattern
/// - parameters:
/// - pattern: string to recognize
/// - return: true for found
public func contains(pattern: String) -> Bool
/// find string ranges
/// - parameters:
/// - pattern: string to recognize
/// - return: a string range array
public func match(pattern: String) -> [Range<String.Index>]
}
我们正在过渡到使用 JIRA 处理所有错误和支持相关问题,因此 GitHub 问题已被禁用。
如果您发现文档中有错误、缺陷或任何其他有用的建议,请访问 http://jira.perfect.org:8080/servicedesk/customer/portal/1 并提出。
可以在 http://jira.perfect.org:8080/projects/ISS/issues 找到未解决问题的完整列表
有关 Perfect 项目的更多信息,请访问 perfect.org。