MoRegex 让您轻松地在 Swift 中使用正则表达式 (Regular Expression)
iOS 原生的正则表达式并不友好,我们经常只想做一些简单的字符串判断,但需要很多代码才能完成,
MoRegex 将这些繁琐的代码封装成一个易于使用的运算符 (Operator) 判断式,
让您只需一行代码就可以做简单的正则表达式判断 (Regular Expressions Matches)
比如我们 UI 上有一个输入框,让用户输入 email,我们必须检查用户输入的是否是正确的 email 格式
例如
let str1 = "02-12345678"
if let res = str1 =~ "(\\d\\d)\\-(\\d\\d\\d\\d\\d\\d\\d\\d)" {
print("1 res=\(res)")
// PRINT: 1 res=["02-12345678", "02", "12345678"]
}
let str2 = "[email protected]"
if let res = str2.regexMatch(check: .mail) { // 電子郵件
// PRINT: res=["[email protected]", "hello-kitty", "mail", "com"]
}
对 String 使用 =~ 运算符操作,会返回一个匹配的数组,如果匹配失败会返回 nil
您可以使用 Swift 包管理器 来安装,在您的 Package.swift
中加入 MoRegex
import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
targets: [],
dependencies: [
.Package(url: "https://github.com/kittymo/MoRegex.git", majorVersion: 1),
]
)
let str1 = "02-12345678" // 想判斷的字串
// 取出前後2組字串
if let res = str1 =~ "(\\d\\d)\\-(\\d\\d\\d\\d\\d\\d\\d\\d)" {
// PRINT: res=["02-12345678", "02", "12345678"]
}
// 取出3組字串
if let res = str1 =~ "(\\d\\d)\\-(\\d\\d\\d\\d)(\\d\\d\\d\\d)" {
// PRINT: res=["02-12345678", "02", "1234", "5678"]
}
// 判斷是否含有 12345
if let res = str1 =~ "12345" {
// PRINT: res=["12345"]
}
// 取出 - 符號後的字串
if let res = str1 =~ "\\-(.*+)" {
// PRINT: res=["-12345678", "12345678"]
}
regexMatch 的功能与 =~ 运算符相同
let str1 = "02-12345678"
if let res = str1.regexMatch("(\\d\\d)\\-(\\d\\d\\d\\d)(\\d\\d\\d\\d)") {
// PRINT: res=["02-12345678", "02", "1234", "5678"]
}
regexReplace 可以让您以模板字符串的方式重新组合取出的字符串
let str1 = "02-12345678"
if let res = str1.regexReplace("(\\d\\d)\\-(\\d\\d\\d\\d)(....)", template: "($1)$2-$3") {
// PRINT: res=(02)1234-5678
}
regexMatchSub 允许您传入一个字符串数组,并按顺序替换掉已匹配的字符串
let str1 = "02-12345678"
if let res = str1.regexMatchSub("(\\d\\d)\\-(\\d\\d\\d\\d)(....)", replaces: ["AB", nil, "CDE"]) {
// PRINT: res=["AB-1234CDE", "02", "1234", "5678"]
// res[0] "AB-1234CDE" 是替換後的結果
}
将输入的 "02-12345678" 经过匹配替换后变成 "AB-1234CDE"
MoRegex 已经内置了几个常用的表达式,您可以用更简单的方式做这些常用的判断
let str2 = "[email protected]"
if let res = str2.regexMatch(check: .mail) { // 電子郵件
// PRINT: res=["[email protected]", "hello-kitty", "mail", "com"]
}
let str3 = "2017-09-30"
if let res = str3.regexMatch(check: .date) { // 日期 年-月-日
// PRINT: res=["2017-09-30", "2017", "09", "30"]
}
let str4 = "02-12341234"
if let res = str4.regexMatch(check: .telphone) { // 市話
// PRINT: res=["02-12341234", "02", "1234", "1234"]
}
let str5 = "0901-123123"
if let res = str5.regexMatch(check: .mobile) { // 行動電話
// PRINT: res=["0901-123123", "0901", "123", "123"]
}
let str6 = "<center>TEST</center>"
if let res = str6.regexMatch(check: .htmlTag) { // HTML標籤
// PRINT: res=["<center>TEST</center>", "center", "", "TEST"]
}
let str7 = "21:05:43"
if let res = str7.regexMatch(check: .time) { // 時間(24小時制)
// PRINT: res=["21:05:43", "21", "05", "43"]
}
let str9 = "https://hello.kitty.com/index.html"
if let res = str9.regexMatch(check: .url) { // 網址
// PRINT: res=["https://hello.kitty.com/index.html", "https://", "hello.kitty", "com", ""]
}
let str10 = "#F390CC"
if let res = str10.regexMatch(check: .colorHex) { // 色碼
// PRINT: res=["#F390CC", "F390CC"]
}
let str11 = "-12345"
if let res = str11.regexMatch(check: .number) { // 整數數字
// PRINT: res=["-12345"]
}
上面的检查示例也可以用 regexCheck 来完成
let str12 = "-12345"
if let res = str12.regexCheck(.number) { // 整數數字
print("res=\(res)")
// PRINT: res=["-12345"]
}
MoRegex 封装了 iOS 的 NSRegularExpression 函数,因此也可以传入 NSRegularExpression 的选项
以区分大小写为例
let str8 = "My name is Kitty"
// 若沒加 options 參數, 則預設文字比對為不區分大小寫 options: [.caseInsensitive]
if let res = str8.regexMatch("kitty") {
// PRINT: res=["Kitty"]
}
if let res = str8.regexMatch("kitty", options: []) { // 區分大小寫
// 因為匹配不成功, 這裡不會被執行
} else {
// PRINT: NOT MATCH
}