Go 每日一庫之 commonregex
簡介
有時,我們會遇到一些需要使用字符串的匹配和查找的任務。并且我們知道這種情況下,使用正則表達式是最簡潔和優(yōu)雅的。為了完成某個任務特地去系統(tǒng)地學習正則表達式費時費力,而且一段時間不用又很容易遺忘。下次遇到問題還要再重復這個過程。commonregex庫來了,它內置很多常用的正則表達式,開箱即用。當然,我并不是說沒必要去學習正則表達式,熟練掌握正則表達式需要時間和練習,對于時長和文本處理打交道的開發(fā)人員,正則表達式?jīng)Q定是提升工作效率的一把利器。
快速使用
本文代碼使用 Go Modules。
創(chuàng)建目錄并初始化:
$ 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?harold.smith@gmail.com`
??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 個以.分隔的數(shù)字,每個數(shù)字都在[0-255]范圍內。
MAC 是計算機的物理地址(又叫以太網(wǎng)地址,局域網(wǎng)地址等),是 6 組以:分隔的十六進制數(shù)字,每組兩個。
MD5 是一種哈希算法,將一段數(shù)據(jù)轉為長度為 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足夠我們去應付一般的使用場景了。
大家如果發(fā)現(xiàn)好玩、好用的 Go 語言庫,歡迎到 Go 每日一庫 GitHub 上提交 issue?
參考
commonregex GitHub:https://github.com/mingrammer/commonregex Go 每日一庫 GitHub:https://github.com/darjun/go-daily-lib
推薦閱讀
站長 polarisxu
自己的原創(chuàng)文章
不限于 Go 技術
職場和創(chuàng)業(yè)經(jīng)驗
Go語言中文網(wǎng)
每天為你
分享 Go 知識
Go愛好者值得關注
