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

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 main

import (
    "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 main

import (
    "fmt"
    "regexp"
    "strings"
)

func fuzzyMatch(pattern, text string) bool {
    // 将 pattern 拆分为多个部分
    parts := strings.Split(pattern, "")
    // 构建正则表达式模式
    var regexParts []string
    for _, 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.kler.cn/news/368386.html

相关文章:

  • 桌面运维SOP
  • Python实现摇号系统
  • 如何在SpringTask的定时任务中创建动态的定时任务
  • MySQL主主SQL线程异常修复大作战,一失足成千古恨啊!
  • OpenStack将运行的系统导出 QCOW2 镜像并导入阿里云
  • Java Web项目实战:从零基础到项目开发全流程
  • NAT技术和代理服务器
  • Docker 下备份恢复oracle
  • FineReport 分栏报表
  • uniapp使用uni-push模拟推送
  • MySQL 【正则表达式】函数大全
  • 如何用Jmeter做性能测试
  • 构建ECMAScript标准
  • 论文略读Fewer Truncations Improve Language Modeling
  • 玩转springboot之springboot属性绑定原理
  • ESP32-S3-DevKitC-1开发记录帖(2)——与MPU6050一起部署动作识别神经网络
  • 单链表总结
  • Zig 语言通用代码生成器:逻辑,冒烟测试版发布二
  • 学习qmake
  • 配置smaba (Linux与windows通信)
  • GPT打数模——电商品类货量预测及品类分仓规划
  • LeetCode9:回文数
  • golang将指针传给cgo后还能被回收吗?
  • 15-4连续子串和的整除问题
  • Android Studio历史版本下载
  • 开源模型应用落地-Qwen2-VL-7B-Instruct-vLLM-OpenAI API Client调用