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 = "hello-kitty@mail.com"
if let res = str2.regexMatch(check: .mail) { // 電子郵件
// PRINT: res=["hello-kitty@mail.com", "hello-kitty", "mail", "com"]
}
对 String 使用 =~ 运算符操作,会回传一个匹配的数组,如果匹配失败会回传 nil
你可以使用 CocoaPods 来安装,把MoRegex
加到你的Podfile
platform :ios, '9.0'
use_frameworks!
target 'MyApp' do
pod 'MoRegex'
end
你可以使用 The Swift Package Manager 来安装,在你的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 = "hello-kitty@mail.com"
if let res = str2.regexMatch(check: .mail) { // 電子郵件
// PRINT: res=["hello-kitty@mail.com", "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 的 options 选项
以区分大小写来做示范
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
}