千家信息网

Go语言正则表达式实例分析

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要介绍了Go语言正则表达式实例分析的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Go语言正则表达式实例分析文章都会有所收获,下面我们一起来看看吧。前言在计算中,
千家信息网最后更新 2026年11月01日Go语言正则表达式实例分析

这篇文章主要介绍了Go语言正则表达式实例分析的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Go语言正则表达式实例分析文章都会有所收获,下面我们一起来看看吧。

前言

在计算中,我们经常需要将特定模式的字符或字符子集匹配为另一个字符串中的字符串。此技术用于使用特别的语法来搜索给定字符串中的特定字符集。

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

什么是正则表达式

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

正则表达式可以用于文本搜索和更高级的文本操作。正则表达式内置于 grep 和 sed 等工具,vi 和 emacs 等文本编辑器,Go、Java 和 Python 等编程语言中。表达式的语法主要遵循这些流行语言中使用的已建立的 RE2 语法。 RE2 语法是 PCRE 的一个子集,有各种注意事项。

MatchString 函数

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

package mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("fmt""os""regexp")func main() {var content = `Foxes are omnivorous mammals belonging to several generaof the family Canidae. Foxes have a flattened skull, upright triangular ears,a pointed, slightly upturned snout, and a long bushy tail. Foxes live on everycontinent except Antarctica. By far the most common and widespread species offox 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 mainimport ("fmt""regexp")func main() {var content = `Foxes are omnivorous mammals belonging to several generaof the family Canidae. Foxes have a flattened skull, upright triangular ears,a pointed, slightly upturned snout, and a long bushy tail. Foxes live on everycontinent except Antarctica. By far the most common and widespread species offox 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

Split 函数

Split 函数将字符串切割成由定义的正则表达式分隔的子字符串。它返回这些表达式匹配之间的子字符串切片。

package mainimport ("fmt""log""regexp""strconv")func main() {var data = `22, 1, 3, 4, 5, 17, 4, 3, 21, 4, 5, 1, 48, 9, 42`sum := 0re := regexp.MustCompile(",\s*")vals := re.Split(data, -1)for _, val := range vals {n, err := strconv.Atoi(val)sum += nif err != nil {log.Fatal(err)}}fmt.Println(sum)}

在代码示例中,我们有一个逗号分隔的值列表。我们从字符串中截取值并计算它们的总和。

re := regexp.MustCompile(",\s*")

正则表达式包括一个逗号字符和任意数量的相邻空格。

vals := re.Split(data, -1)

我们得到了值的一部分。

for _, val := range vals {n, err := strconv.Atoi(val)sum += nif err != nil {log.Fatal(err)}}

我们遍历切片并计算总和。切片包含字符串;因此,我们使用 strconv.Atoi 函数将每个字符串转换为整数。

运行代码:

189

Go 正则表达式捕获组

圆括号 () 用于创建捕获组。这允许我们将量词应用于整个组或将交替限制为正则表达式的一部分。为了找到捕获组(Go 使用术语子表达式),我们使用 FindStringSubmatch 函数。

package mainimport ("fmt""regexp")func main() {websites := [...]string{"webcode.me", "zetcode.com", "freebsd.org", "netbsd.org"}re := regexp.MustCompile("(\w+)\.(\w+)")for _, website := range websites {parts := re.FindStringSubmatch(website)for i, _ := range parts {fmt.Println(parts[i])}fmt.Println("---------------------")}}

在代码示例中,我们使用组将域名分为两部分。

re := regexp.MustCompile("(\w+)\.(\w+)")

我们用括号定义了两个组。

parts := re.FindStringSubmatch(website)

FindStringSubmatch 返回包含匹配项的字符串切片,包括来自捕获组的字符串。

运行代码:

$ go run capturegroups.go
webcode.me
webcode
me
---------------------
zetcode.com
zetcode
com
---------------------
freebsd.org
freebsd
org
---------------------
netbsd.org
netbsd
org
---------------------

正则表达式替换字符串

可以用 ReplaceAllString 替换字符串。该方法返回修改后的字符串。

package mainimport ("fmt""io/ioutil""log""net/http""regexp""strings")func main() {resp, err := http.Get("http://webcode.me")if err != nil {log.Fatal(err)}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {log.Fatal(err)}content := string(body)re := regexp.MustCompile("<[^>]*>")replaced := re.ReplaceAllString(content, "")fmt.Println(strings.TrimSpace(replaced))}

该示例读取网页的 HTML 数据并使用正则表达式去除其 HTML 标记。

resp, err := http.Get("http://webcode.me")

我们使用 http 包中的 Get 函数创建一个 GET 请求。

body, err := ioutil.ReadAll(resp.Body)

我们读取响应对象的主体。

re := regexp.MustCompile("<[^>]*>")

这个模式定义了一个匹配 HTML 标签的正则表达式。

replaced := re.ReplaceAllString(content, "")

我们使用 ReplaceAllString 方法删除所有标签。

ReplaceAllStringFunc 函数

ReplaceAllStringFunc 返回一个字符串的副本,其中正则表达式的所有匹配项都已替换为指定函数的返回值。

package mainimport ("fmt""regexp""strings")func main() {content := "an old eagle"re := regexp.MustCompile(`[^aeiou]`)fmt.Println(re.ReplaceAllStringFunc(content, strings.ToUpper))}

在代码示例中,我们将 strings.ToUpper 函数应用于字符串的所有字符。

$ go run replaceallfunc.goaN oLD eaGLe

关于"Go语言正则表达式实例分析"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"Go语言正则表达式实例分析"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

表达式 正则 字符 函数 字符串 代码 示例 文本 搜索 语言 模式 语法 编译 运行 实例 实例分析 分析 成功 子集 对象 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 百单网互联网科技有限公司怎么样 华服互联网科技靠谱吗 国智互联网科技 A股互联网金融科技公司 互联网扶贫科技 没上市的科技互联网公司 中资互联网科技 互联网科技论文 新华互联网科技校企合作 互联网科技博主是什么意思 成都互联网科技公司100强 互联网科技教育视频 现代互联网科技股 宁夏众和广鑫互联网科技有限公司 世界互联网科技公司市值排名 互联网保险科技创业 高科技互联网套路 普陀区互联网教育科技管理办法 宁波允成互联网科技有限公司 现代科技的标志之一的互联网 互联网属科技 亿策互联网科技 北京禾田雨橡互联网科技 贵州叶脉互联网科技有限公司所属行业 受益于互联网科技的高速发展 互联网科技公司有风险吗 企晟互联网信息科技有限公司 数字科技和互联网金融哪个好 承德科技产业互联网平台 北京瑞云互联网科技有限公司
0