介紹
在我們的日常開發(fā)中,有時(shí)候會(huì)遇到一些需要處理字符串的任務(wù)策橘。并且我們也知道這是需要正則表達(dá)式的窗悯,但是礙于系統(tǒng)地學(xué)習(xí)正則表達(dá)式費(fèi)時(shí)費(fèi)力,而且一段時(shí)間不使用又很容易遺忘梦皮。最近找到了go相關(guān)的一個(gè)正則表達(dá)式第三方庫——commonregex炭分。當(dāng)然,我并不是說沒必要去學(xué)習(xí)正則表達(dá)式剑肯。
使用
創(chuàng)建目錄并初始化
mkdir commonregex && cd commonregex
go mod init 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) // ['Jan 9th 2012']
fmt.Println("time list:", timeList) // ['5:00PM', '4:00']
fmt.Println("link list:", linkList) // ['www.linkedin.com', 'harold.smith@gmail.com']
fmt.Println("phone list:", phoneList) // ['(519)-236-2723x341']
fmt.Println("email list:", emailList) // ['harold.smith@gmail.com']
}
commonregex 庫的接口非常容易使用捧毛。這里列出官方支持的正則表達(dá)式:
Date
Time
Phone
Phones with exts
Link
IPv4
IPv6
IP
Ports without well-known (not known ports)
Price
Hex color
Credit card
VISA credit card
MC credit card
ISBN 10/13
BTC address
Street address
Zip code
Po box
SSN
MD5
SHA1
SHA256
GUID
MAC address
IBAN
Git Repository
2022.3.4 23:13 深圳