golang常用正则表达式集合插件库commonregex的使用

Golang常用正则表达式集合插件库commonregex的使用

CommonRegex是一个Go语言常用的正则表达式集合库,提供了多种常见模式的匹配功能。

安装

go get github.com/mingrammer/commonregex

使用示例

import (
    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)
    // ['Jan 9th 2012']
    timeList := cregex.Time(text)
    // ['5:00PM', '4:00']
    linkList := cregex.Links(text)
    // ['www.linkedin.com', 'harold.smith@gmail.com']
    phoneList := cregex.PhonesWithExts(text)  
    // ['(519)-236-2723x341']
    emailList := cregex.Emails(text)
    // ['harold.smith@gmail.com']
}

功能特性

CommonRegex提供了以下常见模式的匹配功能:

  • 日期(Date)
  • 时间(Time)
  • 电话号码(Phone)
  • 带分机号的电话号码(Phones with exts)
  • 链接(Link)
  • 电子邮件(Email)
  • IPv4地址
  • IPv6地址
  • IP地址(IPv4/IPv6)
  • 非知名端口号(Ports without well-known)
  • 价格(Price)
  • 十六进制颜色值(Hex color)
  • 信用卡号(Credit card)
  • VISA信用卡号
  • 万事达信用卡号(MC credit card)
  • ISBN 10/13
  • BTC地址
  • 街道地址(Street address)
  • 邮政编码(Zip code)
  • 邮政信箱(Po box)
  • 社会安全号码(SSN)
  • MD5哈希值
  • SHA1哈希值
  • SHA256哈希值
  • GUID
  • MAC地址
  • IBAN银行账号
  • Git仓库地址

许可证

该项目采用MIT许可证。


更多关于golang常用正则表达式集合插件库commonregex的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang常用正则表达式集合插件库commonregex的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang常用正则表达式库commonregex使用指南

commonregex是一个Go语言中常用的正则表达式集合库,它预定义了许多常用的正则表达式模式,可以方便地处理各种常见文本匹配需求,如邮箱、URL、IP地址、日期等。

安装commonregex

go get -u github.com/mingrammer/commonregex

基本使用示例

package main

import (
	"fmt"
	"github.com/mingrammer/commonregex"
)

func main() {
	text := `John Doe's email is john.doe@example.com. 
	Visit our website at https://example.com or call 555-123-4567. 
	Today is 2023-08-15. My IP is 192.168.1.1.`

	// 创建commonregex实例
	cr := commonregex.CommonRegex{Text: text}

	// 提取电子邮件
	emails := cr.Emails()
	fmt.Println("Emails:", emails) // [john.doe@example.com]

	// 提取URL
	urls := cr.URLs()
	fmt.Println("URLs:", urls) // [https://example.com]

	// 提取电话号码
	phones := cr.Phones()
	fmt.Println("Phones:", phones) // [555-123-4567]

	// 提取日期
	dates := cr.Dates()
	fmt.Println("Dates:", dates) // [2023-08-15]

	// 提取IP地址
	ips := cr.IPs()
	fmt.Println("IPs:", ips) // [192.168.1.1]
}

commonregex支持的正则表达式类型

commonregex库提供了多种预定义的正则表达式模式:

  1. 日期和时间

    • Dates() - 匹配日期 (YYYY-MM-DD, MM/DD/YYYY等格式)
    • Times() - 匹配时间 (HH:MM:SS格式)
  2. 联系信息

    • Emails() - 匹配电子邮件地址
    • Phones() - 匹配电话号码
    • Links() - 匹配超链接
  3. 网络相关

    • IPs() - 匹配IPv4地址
    • IPv6s() - 匹配IPv6地址
    • URLs() - 匹配URL
    • BTCs() - 匹配比特币地址
    • ETHs() - 匹配以太坊地址
  4. 代码相关

    • HexColors() - 匹配十六进制颜色代码
    • CreditCards() - 匹配信用卡号
    • StreetAddresses() - 匹配街道地址
  5. 其他

    • Prices() - 匹配价格
    • Hashtags() - 匹配标签
    • Mentions() - 匹配提及(@username)

高级用法

自定义文本处理

text := "Custom text with info: test@example.com, 192.168.0.1"
cr := commonregex.CommonRegex{Text: text}
emails := cr.Emails()
ips := cr.IPs()

直接使用正则表达式模式

你也可以直接使用库中定义的正则表达式模式字符串:

import "github.com/mingrammer/commonregex"

// 获取预定义的正则表达式模式
emailPattern := commonregex.Patterns["email"]
phonePattern := commonregex.Patterns["phone"]

// 然后可以使用标准regexp包进行匹配

处理多行文本

longText := `First line with info: john@example.com
Second line: 10.0.0.1
Third line: 2023-01-01`

cr := commonregex.CommonRegex{Text: longText}
fmt.Println(cr.Emails())  // [john@example.com]
fmt.Println(cr.IPs())     // [10.0.0.1]
fmt.Println(cr.Dates())   // [2023-01-01]

性能考虑

commonregex库使用Go标准库中的regexp包实现,对于大多数常见用例性能足够。但如果处理非常大的文本或性能敏感场景,可能需要考虑:

  1. 预编译常用正则表达式
  2. 避免重复创建commonregex实例
  3. 对于简单模式,考虑直接使用字符串函数而非正则

总结

commonregex是一个方便的Go库,它封装了许多常用的正则表达式模式,可以显著减少开发者在处理常见文本匹配任务时的工作量。通过使用这个库,你可以避免重复编写和维护复杂的正则表达式,同时保证匹配模式的准确性和一致性。

对于更复杂的文本处理需求,你可能需要结合使用commonregex和标准regexp包,或者编写自定义的正则表达式。

回到顶部