> 本章介紹正則表達式,使用這種模式可以實現對文本內容 校驗、解析、替換
[TOC]
# 獲取解析對象
> 可通過Compile() 和 MushCompile()來獲取到解析對象,并進行下一步的正則匹配
## Compile()
> 返回 Regexp 對象以及是否錯誤信息
~~~
text := `wang666`
// 如果解析出錯,則會panic
re, err := regexp.Compile(`[a-z]+`)
if err != nil {
return
}
match := re.FindString(text)
fmt.Println(match)
~~~
## MustCompile()
> 返回 Regexp,解析表達式失敗則會panic
~~~
text := `wang666`
// 如果解析出錯,則會panic
re := regexp.MustCompile(`[a-z]+`)
match := re.FindString(text)
fmt.Println(match)
~~~
# 校驗是否匹配
~~~
text := `111@qq.com`
re := regexp.MustCompile(`^(\w+)@(\w+)\.(\w{3,5})$`)
result := re.MatchString(text)
if result == true{
fmt.Println("available")
}else {
fmt.Println("unavailable")
}
~~~
# 查找匹配內容
## 正則匹配單個值
### FindString() 匹配單個值
> 如果沒有匹配的話,返回 空字符串
~~~
text := `my email is 111@qq.com`
re := regexp.MustCompile(`\w+@\w+\.\w{3,5}$`)
match := re.FindString(text)
fmt.Println(match)
~~~
> 運行結果
~~~
111@qq.com
~~~
### FindStringSubmatch() 提取分組內容
> 就是提取正則表達式括號里的內容,如果沒有匹配的話,返回 nil
~~~
text := `my email is 111@qq.com`
re := regexp.MustCompile(`(\w+)@(\w+)\.(\w{3,5})$`)
match := re.FindStringSubmatch(text)
fmt.Println(match)
fmt.Println("--------------------")
fmt.Printf("value %v , name=%v\n", match, match[1])
~~~
> 運行結果
~~~
value [111@qq.com 111 qq com] , name=111
~~~
## 正則匹配多個值
### FindAllString() 匹配多值
> 如果沒有匹配的話,返回 nil
~~~
text := `my email is 111@qq.com
my email2 is 222@qq.com`
re := regexp.MustCompile(`\w+@\w+\.\w{3,5}`)
matches := re.FindAllString(text, -1)
fmt.Println(matches)
fmt.Println("--------------------")
for _, value := range matches {
fmt.Println(value)
}
~~~
> 運行結果
~~~
[111@qq.com 222@qq.com]
--------------------
111@qq.com
222@qq.com
~~~
### FindAllStringSubmatch() 提取分組內容
> 就是提取正則表達式括號里的內容,如果沒有匹配的話,返回 nil
~~~
text := `my email is 111@qq.com
my email2 is 222@qq.com`
re := regexp.MustCompile(`(\w+)@(\w+)\.(\w{3,5})`)
matches := re.FindAllStringSubmatch(text, -1)
fmt.Println(matches)
fmt.Println("--------------------")
for _, value := range matches {
fmt.Printf("value %v , name=%v\n", value, value[1])
}
~~~
> 運行結果
~~~
[[111@qq.com 111 qq com] [222@qq.com 222 qq com]]
--------------------
value [111@qq.com 111 qq com] , name=111
value [222@qq.com 222 qq com] , name=222
~~~
# 替換內容
> 使用ReplaceAllString() 進行全文本替換
~~~
text := `my email is 111@qq.com
my email2 is 222@qq.com`
re := regexp.MustCompile(`\w+@\w+\.\w{3,5}`)
newstr := re.ReplaceAllString(text, "***")
// 還可以只替換分組部分,如下
//newstr := re.ReplaceAllString(text, `***@$2.$3`)
fmt.Println(newstr)
~~~
> 運行結果
~~~
my email is ***
my email2 is ***
~~~
> 使用ReplaceAllStringFunc() 進行全文本替換
~~~
text := `my email is 111@qq.com
my email2 is 222@qq.com`
re := regexp.MustCompile(`(\w+)@(\w+)\.(\w{3,5})`)
newstr := re.ReplaceAllStringFunc(text, func(s string) string {
str := s
if s == "111@qq.com" {
str = "[data is encrypted]"
}
return str
})
fmt.Println(newstr)
~~~
> 運行結果
~~~
my email is [data is encrypted]
my email2 is 222@qq.com
~~~
- 基礎知識
- 開發環境
- 包名規則
- 包初始化 (init)
- 基礎數據類型
- 基礎類型轉換
- 格式化輸出
- go指針
- 流程控制語句
- 函數定義
- 匿名函數
- 數組和切片
- map集合
- 結構體
- Interface接口
- 日期處理
- 數學計算
- 正則表達式
- 協程 (并發處理)
- channel
- waitgroup
- mutex (鎖機制)
- websocket
- protobuf
- Redis
- 錯誤處理
- 打包程序
- NSQ消息隊列
- 單元測試
- beego
- 安裝入門
- Gin
- 快速入門
- 路由與控制器
- 處理請求參數
- 表單驗證
- 處理響應結果
- 渲染HTML模版
- 訪問靜態文件
- Gin中間件
- Cookie處理
- Session處理
- Gin上傳文件
- swagger
- pprof性能測試
- GORM
- 入門教程
- 模型定義
- 數據庫連接
- 插入數據
- 查詢數據
- 更新數據
- 刪除數據
- 事務處理
- 關聯查詢
- 屬于 (BELONG TO)
- 一對一 (Has One)
- 一對多 (Has Many)
- 多對多 (Many to Many)
- 預加載 (Preloading)
- 錯誤處理
- 第三方常用插件
- viper 讀取配置文件
- zap 高性能日志
- Nginx代理配置
- Goland 快捷鍵