【GoCN酷Go推薦】Go 正則表達(dá)式庫之 commonregex
為什么使用 commonregex?
在開發(fā)的時候,我們會遇到一些需要使用字符串的匹配和查找的任務(wù)。我們可以使用正則表達(dá)式去提取感興趣的數(shù)據(jù),如手機(jī)號碼,郵件,超鏈接等。但是正則表達(dá)式寫起來費時費力,而且容易遺忘。commonregex 它提供了很多內(nèi)置的正則表達(dá)式,開箱即用,能極大的提高開發(fā)體驗和開發(fā)效率。
commonregex 簡介
提供經(jīng)常使用的正則表達(dá)式的集合。
它提供了這些作為獲取與特定模式對應(yīng)的匹配字符串的簡單函數(shù)。
日期 時間 電話號碼 超鏈接 郵件地址 IPv4/IPv6/IP 地址 價格 十六進(jìn)制顏色值 信用卡卡號 10/13 位 ISBN 郵政編碼 MD5 SHA1 SHA256 GUID,全局唯一標(biāo)識 Git 倉庫地址
快速使用 commonregex
安裝 commonregex
go get -u github.com/mingrammer/commonregex
簡單使用 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)
}
運行結(jié)果:
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 非常易于使用,調(diào)用相應(yīng)的類別方法返回一段文本中符合這些格式的字符串列表。
上面依次從text獲取日期列表,時間列表,超鏈接列表,電話號碼列表和電子郵件列表。
總結(jié)
commonregex 提供了常用的正則表達(dá)式的函數(shù),足以應(yīng)付我們?nèi)粘i_發(fā)場景,能較大的提高我們的開發(fā)效率。
參考資料
https://github.com/mingrammer/commonregex https://darjun.github.io/2020/09/05/godailylib/commonregex/
更多請查看:https://github.com/mingrammer/commonregex
歡迎加入我們GOLANG中國社區(qū):https://gocn.vip/
《酷Go推薦》招募:
各位Gopher同學(xué),最近我們社區(qū)打算推出一個類似GoCN每日新聞的新欄目《酷Go推薦》,主要是每周推薦一個庫或者好的項目,然后寫一點這個庫使用方法或者優(yōu)點之類的,這樣可以真正的幫助到大家能夠?qū)W習(xí)到
新的庫,并且知道怎么用。
大概規(guī)則和每日新聞類似,如果報名人多的話每個人一個月輪到一次,歡迎大家報名?。▓竺刂罚篽ttps://wj.qq.com/s2/7734329/3f51)
掃碼也可以加入 GoCN 的大家族喲~
Gopher China2021大會日程詳情來了!
想和各位技術(shù)大佬們同臺見面嘛?
那就趕快點擊下方「閱讀原文」報名參加呀!
