前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go 每日一库之 commonregex

Go 每日一库之 commonregex

作者头像
用户7731323
发布2020-09-17 14:37:53
5320
发布2020-09-17 14:37:53
举报
文章被收录于专栏:GoUpUpGoUpUp

简介

有时,我们会遇到一些需要使用字符串的匹配和查找的任务。并且我们知道这种情况下,使用正则表达式是最简洁和优雅的。为了完成某个任务特地去系统地学习正则表达式费时费力,而且一段时间不用又很容易遗忘。下次遇到问题还要再重复这个过程。commonregex库来了,它内置很多常用的正则表达式,开箱即用。当然,我并不是说没必要去学习正则表达式,熟练掌握正则表达式需要时间和练习,对于时长和文本处理打交道的开发人员,正则表达式决定是提升工作效率的一把利器。

快速使用

本文代码使用 Go Modules。

创建目录并初始化:

代码语言:javascript
复制
$ mkdir commonregex && cd commonregex
$ go mod init github.com/darjun/go-daily-lib/commonregex

安装commonregex库:

代码语言:javascript
复制
$ go get -u github.com/mingrammer/commonregex

简单使用:

代码语言:javascript
复制
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)
}

运行结果:

代码语言:javascript
复制
$ go run main.go
date list: [Jan 9th 2012]
time list: [5:00PM 4:00 ]
link list: [www.linkedin.com harold.smith@gmail.com]
phone list: [(519)-236-2723x341]
email list: [harold.smith@gmail.com]

commonregex提供的 API 非常易于使用,调用相应的类别方法返回一段文本中符合这些格式的字符串列表。上面依次从text获取日期列表时间列表超链接列表电话号码列表电子邮件列表

内置的正则

commonregex支持很多常用的正则表达式:

  • 日期;
  • 时间;
  • 电话号码;
  • 超链接;
  • 邮件地址;
  • IPv4/IPv6/IP 地址;
  • 价格;
  • 十六进制颜色值;
  • 信用卡卡号;
  • 10/13 位 ISBN;
  • 邮政编码;
  • MD5;
  • SHA1;
  • SHA256;
  • GUID,全局唯一标识;
  • Git 仓库地址。

每种类型又支持多种格式,例如日期支持09.11.2020/Sep 11th 2020

下面挑选几种类型来介绍。

日期

代码语言:javascript
复制
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 的输出):

代码语言:javascript
复制
[09.11.2020 Sep 11th 2020]

时间

时间相对来说格式单一一些,有 24 小时制的时间如:08:30/14:35,有 12 小时制的时间:08:30am/02:35pm

看示例:

代码语言:javascript
复制
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)
}

匹配出来的时间列表:

代码语言:javascript
复制
[08:30  08:30am 13:00 01:00pm]

IP/MAC/MD5

使用方法都是类似的,这几个放在一起举例。

IPv4 地址是 4 个以.分隔的数字,每个数字都在[0-255]范围内。

MAC 是计算机的物理地址(又叫以太网地址,局域网地址等),是 6 组以:分隔的十六进制数字,每组两个。

MD5 是一种哈希算法,将一段数据转为长度为 32 的字符串。

代码语言:javascript
复制
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)
}

输出:

代码语言:javascript
复制
mac list: [ac:de:48:00:11:22]
ip list: [192.168.3.20]
md5 list: [fdbf72fdabb67ea6ef7ff5155a44def4]

总结

commonregex足够我们去应付一般的使用场景了。

大家如果发现好玩、好用的 Go 语言库,欢迎到 Go 每日一库 GitHub 上提交 issue?

参考

  1. commonregex GitHub:https://github.com/mingrammer/commonregex
  2. Go 每日一库 GitHub:https://github.com/darjun/go-daily-lib

我的博客:https://darjun.github.io

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-09-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 GoUpUp 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 快速使用
  • 内置的正则
    • 日期
      • 时间
        • IP/MAC/MD5
        • 总结
        • 参考
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档