介绍
在我们的日常开发中,有时候会遇到一些需要处理字符串的任务。并且我们也知道这是需要正则表达式的,但是碍于系统地学习正则表达式费时费力,而且一段时间不使用又很容易遗忘。最近找到了go相关的一个正则表达式第三方库——commonregex。当然,我并不是说没必要去学习正则表达式。
使用
创建目录并初始化
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 库的接口非常容易使用。这里列出官方支持的正则表达式:
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 深圳