Go 语言入门很简单:正则表达式(上)

简介: 在计算中,我们经常需要将特定模式的字符或字符子集匹配为另一个字符串中的字符串。此技术用于使用特别的语法来搜索给定字符串中的特定字符集。比如邮件、手机号、身份证号等等。如果搜索到的模式匹配,或者在目标字符串中找到给定的子集,则搜索被称为成功;否则被认为是不成功的。那么此时该用到正则表达式了。

网络异常,图片无法展示
|


前言

在计算中,我们经常需要将特定模式的字符或字符子集匹配为另一个字符串中的字符串。此技术用于使用特别的语法来搜索给定字符串中的特定字符集。比如邮件、手机号、身份证号等等。

如果搜索到的模式匹配,或者在目标字符串中找到给定的子集,则搜索被称为成功;否则被认为是不成功的。

那么此时该用到正则表达式了。

什么是正则表达式

正则表达式(或 RegEx)是一个特殊的字符序列,它定义了用于匹配特定文本的搜索模式。在 Golang 中,有一个内置的正则表达式包:  regexp  包,其中包含所有操作列表,如过滤、修改、替换、验证或提取。

正则表达式可以用于文本搜索和更高级的文本操作。正则表达式内置于 grep 和 sed 等工具,vi 和 emacs 等文本编辑器,Go、Java 和 Python 等编程语言中。


表达式的语法主要遵循这些流行语言中使用的已建立的 RE2 语法。 RE2 语法是 PCRE 的一个子集,有各种注意事项。


以下是正则表达式模式表格:


网络异常,图片无法展示
|


Go 语言的 regexp 包中有几个典型的函数:

  • MatchString()
  • Compile()
  • FindString()
  • FindAllString()
  • FindStringIndex()
  • FindAllStringIndex()
  • FindStringSubmatch()
  • Split()
  • ReplaceAllString
  • ReplaceAllStringFunc()


现在来一一看看这些函数的使用

MatchString 函数

MatchString() 函数报告作为参数传递的字符串是否包含正则表达式模式的任何匹配项。

 package main
 ​
 import (
   "fmt"
   "log"
   "regexp"
 )
 ​
 func main() {
 ​
   words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}
 ​
   for _, word := range words {
 ​
     found, err := regexp.MatchString(".even", word)
 ​
     if err != nil {
       log.Fatal(err)
     }
 ​
     if found {
 ​
       fmt.Printf("%s matches\n", word)
     } else {
 ​
       fmt.Printf("%s does not match\n", word)
     }
   }
 }
 ​

运行该代码:

 Seven matches
 even does not match
 Maven does not match
 Amen does not match
 eleven matches

但同时我们能看到编辑器有提示:

网络异常,图片无法展示
|

编译器已经开始提醒我们,MatchString 直接使用性能很差,所以考虑使用 regexp.Compile 函数。

Compile 函数

Compile 函数解析正则表达式,如果成功,则返回可用于匹配文本的 Regexp 对象。编译的正则表达式产生更快的代码。

MustCompile 函数是一个便利函数,它编译正则表达式并在无法解析表达式时发生 panic。

 package main
 ​
 import (
   "fmt"
   "log"
   "regexp"
 )
 ​
 func main() {
 ​
   words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}
 ​
   re, err := regexp.Compile(".even")
 ​
   if err != nil {
     log.Fatal(err)
   }
 ​
   for _, word := range words {
 ​
     found := re.MatchString(word)
 ​
     if found {
 ​
       fmt.Printf("%s matches\n", word)
     } else {
 ​
       fmt.Printf("%s does not match\n", word)
     }
   }
 }
 ​

在代码示例中,我们使用了编译的正则表达式。

 re, err := regexp.Compile(".even")

即使用 Compile 编译正则表达式。然后在返回的正则表达式对象上调用 MatchString 函数:

 found := re.MatchString(word)

运行程序,能看到同样的代码:

 Seven matches
 even does not match
 Maven does not match
 Amen does not match
 eleven matches

MustCompile 函数

 package main
 ​
 import (
   "fmt"
   "regexp"
 )
 ​
 func main() {
 ​
   words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}
 ​
   re := regexp.MustCompile(".even")
 ​
   for _, word := range words {
 ​
     found := re.MatchString(word)
 ​
     if found {
 ​
       fmt.Printf("%s matches\n", word)
     } else {
 ​
       fmt.Printf("%s does not match\n", word)
     }
   }
 }

FindAllString 函数

FindAllString 函数返回正则表达式的所有连续匹配的切片。

 package main
 ​
 import (
     "fmt"
     "os"
     "regexp"
 )
 ​
 func main() {
 ​
     var content = `Foxes are omnivorous mammals belonging to several genera 
 of the family Canidae. Foxes have a flattened skull, upright triangular ears, 
 a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every 
 continent except Antarctica. By far the most common and widespread species of 
 fox is the red fox.`
 ​
     re := regexp.MustCompile("(?i)fox(es)?")
 ​
     found := re.FindAllString(content, -1)
 ​
     fmt.Printf("%q\n", found)
 ​
     if found == nil {
         fmt.Printf("no match found\n")
         os.Exit(1)
     }
 ​
     for _, word := range found {
         fmt.Printf("%s\n", word)
     }
 ​
 }

在代码示例中,我们找到了单词 fox 的所有出现,包括它的复数形式。

 re := regexp.MustCompile("(?i)fox(es)?")

使用 (?i) 语法,正则表达式不区分大小写。 (es)?表示“es”字符可能包含零次或一次。

 found := re.FindAllString(content, -1)

我们使用 FindAllString 查找所有出现的已定义正则表达式。第二个参数是要查找的最大匹配项; -1 表示搜索所有可能的匹配项。

运行结果:

 ["Foxes" "Foxes" "Foxes" "fox" "fox"]
 Foxes
 Foxes
 Foxes
 fox
 fox

FindAllStringIndex 函数

 package main
 ​
 import (
     "fmt"
     "regexp"
 )
 ​
 func main() {
 ​
     var content = `Foxes are omnivorous mammals belonging to several genera 
 of the family Canidae. Foxes have a flattened skull, upright triangular ears, 
 a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every 
 continent except Antarctica. By far the most common and widespread species of 
 fox is the red fox.`
 ​
     re := regexp.MustCompile("(?i)fox(es)?")
 ​
     idx := re.FindAllStringIndex(content, -1)
 ​
     for _, j := range idx {
         match := content[j[0]:j[1]]
         fmt.Printf("%s at %d:%d\n", match, j[0], j[1])
     }
 }

在代码示例中,我们在文本中找到所有出现的 fox 单词及其索引。

 Foxes at 0:5
 Foxes at 81:86
 Foxes at 196:201
 fox at 296:299
 fox at 311:314
相关文章
|
3月前
|
Cloud Native 安全 Java
Go语言深度解析:从入门到精通的完整指南
🌟蒋星熠Jaxonic,Go语言探索者。深耕云计算、微服务与并发编程,以代码为笔,在二进制星河中书写极客诗篇。分享Go核心原理、性能优化与实战架构,助力开发者掌握云原生时代利器。#Go语言 #并发编程 #性能优化
500 43
Go语言深度解析:从入门到精通的完整指南
|
3月前
|
存储 安全 Java
【Golang】(4)Go里面的指针如何?函数与方法怎么不一样?带你了解Go不同于其他高级语言的语法
结构体可以存储一组不同类型的数据,是一种符合类型。Go抛弃了类与继承,同时也抛弃了构造方法,刻意弱化了面向对象的功能,Go并非是一个传统OOP的语言,但是Go依旧有着OOP的影子,通过结构体和方法也可以模拟出一个类。
264 1
|
4月前
|
Cloud Native 安全 Java
Go语言深度解析:从入门到精通的完整指南
🌟 蒋星熠Jaxonic,执着的星际旅人,用Go语言编写代码诗篇。🚀 Go语言以简洁、高效、并发为核心,助力云计算与微服务革新。📚 本文详解Go语法、并发模型、性能优化与实战案例,助你掌握现代编程精髓。🌌 从goroutine到channel,从内存优化到高并发架构,全面解析Go的强大力量。🔧 实战构建高性能Web服务,展现Go在云原生时代的无限可能。✨ 附技术对比、最佳实践与生态全景,带你踏上Go语言的星辰征途。#Go语言 #并发编程 #云原生 #性能优化
|
5月前
|
Cloud Native 安全 Java
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
356 1
|
5月前
|
Cloud Native Go API
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
447 0
|
5月前
|
Cloud Native Java Go
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
301 0
|
5月前
|
Cloud Native Java 中间件
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
307 0
|
5月前
|
Cloud Native Java Go
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
366 0
|
5月前
|
数据采集 Go API
Go语言实战案例:多协程并发下载网页内容
本文是《Go语言100个实战案例 · 网络与并发篇》第6篇,讲解如何使用 Goroutine 和 Channel 实现多协程并发抓取网页内容,提升网络请求效率。通过实战掌握高并发编程技巧,构建爬虫、内容聚合器等工具,涵盖 WaitGroup、超时控制、错误处理等核心知识点。
|
5月前
|
数据采集 JSON Go
Go语言实战案例:实现HTTP客户端请求并解析响应
本文是 Go 网络与并发实战系列的第 2 篇,详细介绍如何使用 Go 构建 HTTP 客户端,涵盖请求发送、响应解析、错误处理、Header 与 Body 提取等流程,并通过实战代码演示如何并发请求多个 URL,适合希望掌握 Go 网络编程基础的开发者。