簡介
有時,我們會遇到一些需要使用字元串的比對和查找的任務。并且我們知道這種情況下,使用正規表達式是最簡潔和優雅的。為了完成某個任務特地去系統地學習正規表達式費時費力,而且一段時間不用又很容易遺忘。下次遇到問題還要再重複這個過程。
commonregex
庫來了,它内置很多常用的正規表達式,開箱即用。當然,我并不是說沒必要去學習正規表達式,熟練掌握正規表達式需要時間和練習,對于時長和文本處理打交道的開發人員,正規表達式決定是提升工作效率的一把利器。
快速使用
本文代碼使用 Go Modules。
建立目錄并初始化:
$ mkdir commonregex && cd commonregex
$ go mod init github.com/darjun/go-daily-lib/commonregex
複制
安裝
commonregex
庫:
$ go get -u github.com/mingrammer/commonregex
複制
簡單使用:
package main
import (
"fmt"
cregex "github.com/mingrammer/commonregex"
)
func main() {
text := `John, please get that article on www.linkedin.com to me by 5:00PM on Jan 9th 2012. 4:00 would be ideal, actually. If you have any questions, You can reach me at (519)-236-2723x341 or get in touch with my associate at [email protected]`
dateList := cregex.Date(text)
timeList := cregex.Time(text)
linkList := cregex.Links(text)
phoneList := cregex.PhonesWithExts(text)
emailList := cregex.Emails(text)
fmt.Println("date list:", dateList)
fmt.Println("time list:", timeList)
fmt.Println("link list:", linkList)
fmt.Println("phone list:", phoneList)
fmt.Println("email list:", emailList)
}
複制
運作結果:
$ go run main.go
date list: [Jan 9th 2012]
time list: [5:00PM 4:00 ]
link list: [www.linkedin.com [email protected]]
phone list: [(519)-236-2723x341]
email list: [[email protected]]
複制
commonregex
提供的 API 非常易于使用,調用相應的類别方法傳回一段文本中符合這些格式的字元串清單。上面依次從
text
擷取日期清單,時間清單,超連結清單,電話号碼清單和電子郵件清單。
内置的正則
commonregex
支援很多常用的正規表達式:
- 日期;
- 時間;
- 電話号碼;
- 超連結;
- 郵件位址;
- IPv4/IPv6/IP 位址;
- 價格;
- 十六進制顔色值;
- 信用卡卡号;
- 10/13 位 ISBN;
- 郵政編碼;
- MD5;
- SHA1;
- SHA256;
- GUID,全局唯一辨別;
- Git 倉庫位址。
每種類型又支援多種格式,例如日期支援
09.11.2020
/
Sep 11th 2020
。
下面挑選幾種類型來介紹。
日期
func main() {
text := `commonregex support many date formats, like 09.11.2020, Sep 11th 2020 and so on.`
dateList := commonregex.Date(text)
fmt.Println(dateList)
}
複制
比對出來的日期(注意 Go 中 slice 的輸出):
[09.11.2020 Sep 11th 2020]
複制
時間
時間相對來說格式單一一些,有 24 小時制的時間如:
08:30
/
14:35
,有 12 小時制的時間:
08:30am
/
02:35pm
。
看示例:
func main() {
text := `I wake up at 08:30 (aka 08:30am) in the morning, take a snap at 13:00(aka 01:00pm).`
timeList := commonregex.Time(text)
fmt.Println(timeList)
}
複制
比對出來的時間清單:
[08:30 08:30am 13:00 01:00pm]
複制
IP/MAC/MD5
使用方法都是類似的,這幾個放在一起舉例。
IPv4 位址是 4 個以
.
分隔的數字,每個數字都在[0-255]範圍内。
MAC 是計算機的實體位址(又叫以太網位址,區域網路位址等),是 6 組以
:
分隔的十六進制數字,每組兩個。
MD5 是一種雜湊演算法,将一段資料轉為長度為 32 的字元串。
func main() {
text := `mac address: ac:de:48:00:11:22, ip: 192.168.3.20, md5: fdbf72fdabb67ea6ef7ff5155a44def4`
macList := commonregex.MACAddresses(text)
ipList := commonregex.IPs(text)
md5List := commonregex.MD5Hexes(text)
fmt.Println("mac list:", macList)
fmt.Println("ip list:", ipList)
fmt.Println("md5 list:", md5List)
}
複制
輸出:
mac list: [ac:de:48:00:11:22]
ip list: [192.168.3.20]
md5 list: [fdbf72fdabb67ea6ef7ff5155a44def4]
複制
總結
commonregex
足夠我們去應付一般的使用場景了。
大家如果發現好玩、好用的 Go 語言庫,歡迎到 Go 每日一庫 GitHub 上送出 issue?
參考
- commonregex GitHub:https://github.com/mingrammer/commonregex
- Go 每日一庫 GitHub:https://github.com/darjun/go-daily-lib
我
我的部落格:https://darjun.github.io