当前位置: 首页 > news >正文

golang正则表达式的使用及举例

正则表达式很强大,在一些场合如抓包,爬虫等方面很有用。在 Go语言中,正则表达式通过标准库 regexp 提供支持。使用正则表达式可以进行字符串匹配、替换和分割等操作。

以下是正则表达式的基本使用方法及示例:

1. 导入 regexp 包

在你的 Go 代码中,首先需要导入 regexp 包:

import ("fmt""regexp"
)

2. 编译正则表达式

使用 regexp.Compile 或 regexp.MustCompile 来编译正则表达式。MustCompile 在编译出错时会导致程序崩溃,因此对于已知的正则表达式可直接使用它。

re, err := regexp.Compile("a([a-z]+)e")
if err != nil {fmt.Println("Error compiling regex:", err)
}

或使用:

re := regexp.MustCompile("a([a-z]+)e")
3. 使用正则表达式

编译后的正则表达式可以用于多种功能:

3.1. 匹配字符串

使用 MatchString 函数判断字符串是否符合正则表达式。

matched := re.MatchString("apple")
fmt.Println("Matched:", matched) // 输出: Matched: true
3.2. 查找匹配

使用 FindString 获取匹配的子串。

result := re.FindString("I have an apple here.")
fmt.Println("Found:", result) // 输出: Found: apple
3.3. 查找所有匹配

使用 FindAllString 获取所有匹配的子串。

results := re.FindAllString("I have an apple and an axe here.", -1)
fmt.Println("Found all:", results) // 输出: Found all: [apple axe]
3.4. 替换

使用 ReplaceAllString 替换匹配的子串。

replaced := re.ReplaceAllString("I have an apple and an axe here.", "fruit")
fmt.Println("Replaced:", replaced) // 输出: Replaced: I have an fruit and an fruit here.
3.5. 分割字符串

使用 Split 函数按照正则表达式分割字符串。

splitStrings := re.Split("I have an apple and an axe here.", -1)
fmt.Println("Split:", splitStrings) // 输出: Split: [I have an  and an  here.]

完整示例

下面是一个完整的示例,演示了上述功能:

package mainimport ("fmt""regexp"
)func main() {// 编译正则表达式re := regexp.MustCompile("a([a-z]+)e")// 匹配字符串matched := re.MatchString("apple")fmt.Println("Matched:", matched) // 输出: Matched: true// 找到匹配的字符串result := re.FindString("I have an apple here.")fmt.Println("Found:", result) // 输出: Found: apple// 找到所有匹配的字符串results := re.FindAllString("I have an apple and an axe here.", -1)fmt.Println("Found all:", results) // 输出: Found all: [apple axe]// 替换匹配的子串replaced := re.ReplaceAllString("I have an apple and an axe here.", "fruit")fmt.Println("Replaced:", replaced) // 输出: Replaced: I have an fruit and an fruit here.// 按照正则表达式分割字符串splitStrings := re.Split("I have an apple and an axe here.", -1)fmt.Println("Split:", splitStrings) // 输出: Split: [I have an  and an  here.]
}

要让正则表达式也能匹配到 HelloWorld 这样的字符串,你需要构建一个更灵活的正则表达式,允许字符之间有可选的连字符、空白字符或 · 字符,同时也能匹配连续的字符。

示例代码

package mainimport ("fmt""regexp""strings"
)func fuzzyMatch(pattern, text string) bool {// 将 pattern 拆分为多个部分parts := strings.Split(pattern, "")// 构建正则表达式模式var regexParts []stringfor _, part := range parts {regexParts = append(regexParts, regexp.QuoteMeta(part))}regexPattern := strings.Join(regexParts, `[-\s·]?`)// 构建最终的正则表达式finalPattern := fmt.Sprintf("^.*%s.*$", regexPattern)re := regexp.MustCompile(finalPattern)// 进行匹配return re.MatchString(text)
}func main() {pattern := "HelloWorld"texts := []string{"Hello-World","Hello World","Hello·World","HelloWorld",}for _, text := range texts {if fuzzyMatch(pattern, text) {fmt.Printf("Matched: %s\n", text)} else {fmt.Printf("Not matched: %s\n", text)}}
}

解释

  1. 拆分模式字符串

    • 使用 strings.Split(pattern, "") 将模式字符串拆分为单个字符的切片。
  2. 构建正则表达式模式

    • 使用 regexp.QuoteMeta(part) 对每个字符进行转义,确保它们在正则表达式中被正确处理。
    • 使用 strings.Join(regexParts, [-\s·]?) 将这些字符连接起来,允许它们之间有可选的连字符、空白字符或 · 字符。
  3. 构建最终的正则表达式

    • 使用 fmt.Sprintf("^.*%s.*$", regexPattern) 构建最终的正则表达式,匹配包含模糊模式字符串的任意字符串。
  4. 进行匹配

    • 使用 regexp.MustCompile(finalPattern) 编译正则表达式。
    • 使用 re.MatchString(text) 进行匹配,返回匹配结果。

通过这种方式,你可以实现对包含特定子字符串的字符串进行模糊匹配查找,即使它们之间有空格、标点符号或其他字符,同时也匹配不包含这些字符的字符串。

输出

Matched: Hello-World
Matched: Hello World
Matched: Hello·World
Matched: HelloWorld
http://www.lryc.cn/news/470958.html

相关文章:

  • Flutter杂学: iOS 上启用自动填充和关联域
  • 接口自动化-框架搭建(Python+request+pytest+allure)
  • [论文阅读]Constrained Decision Transformer for Offline Safe Reinforcement Learning
  • 工具_Nginx
  • web开发Model1
  • ImportError: cannot import name ‘Sequential‘ from ‘keras.models‘
  • python实战(二)——房屋价格回归建模
  • UHF机械高频头的知识和待学习的疑问
  • 深入理解 SQL 中的 WITH AS 语法
  • 同三维T80005JEHA-4K60 4K60超高清HDMI/AV解码器
  • 深信服秋季新品重磅发布:安全GPT4.0数据安全大模型与分布式存储EDS新版本520,助力数字化更简单、更安全
  • Flutter图片控件(七)
  • JavaEE初阶---文件IO总结
  • 10.28Python_pandas_csv
  • 数据处理与可视化:pandas 和 matplotlib 初体验(9/10)
  • 鸿蒙学习总结
  • 如何修改文件创建时间?六个超简单修改方法介绍
  • 【MySQL 保姆级教学】内置函数(9)
  • 华为大咖说丨如何通过反馈机制来不断优化大模型应用?
  • 上海亚商投顾:沪指缩量震荡 风电、传媒股集体走强
  • 三磺酸-Cy3.5-羧酸在水相环境中表现良好,能够提高成像的清晰度和准确性
  • 国标GB28181视频平台EasyGBS国标GB28181软件实现无需插件的视频监控对讲和网页直播
  • mac nwjs程序签名公证(其他mac程序也一样适用)
  • 网络应用技术 实验一:路由器实现不同网络间通信(华为ensp)
  • 使用 Qt GRPC 构建高效的 Trojan-Go 客户端:详细指南
  • 【mysql进阶】5-事务和锁
  • 指增和中性产品的申赎加减仓及资金调拨自动化伪代码思路
  • 【论文分享】居住开放空间如何影响老年人的情感:使用可穿戴传感器的现场实验
  • 入门 | Prometheus+Grafana 普罗米修斯
  • 制作Ubuntu根文件系统