Go语言正则表达式示例
作者:NNode 时间:2023-04-13 19:41:34
本文实例讲述了Go语言正则表达式。分享给大家供大家参考,具体如下:
package main
import "bytes"
import "fmt"
import "regexp"
func main() {
//这个测试一个字符串是否符合一个表达式。
match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
fmt.Println(match)
//上面我们是直接使用字符串,但是对于一些其他的正则任务,你需要使用 Compile 一个优化的 Regexp 结构体。
r, _ := regexp.Compile("p([a-z]+)ch")
//这个结构体有很多方法。这里是类似我们前面看到的一个匹配测试。
fmt.Println(r.MatchString("peach"))
//这是查找匹配字符串的。
fmt.Println(r.FindString("peach punch"))
//这个也是查找第一次匹配的字符串的,但是返回的匹配开始和结束位置索引,而不是匹配的内容。
fmt.Println(r.FindStringIndex("peach punch"))
//Submatch 返回完全匹配和局部匹配的字符串。例如,这里会返回 p([a-z]+)ch 和 `([a-z]+) 的信息。
fmt.Println(r.FindStringSubmatch("peach punch"))
//类似的,这个会返回完全匹配和局部匹配的索引位置。
fmt.Println(r.FindStringSubmatchIndex("peach punch"))
//带 All 的这个函数返回所有的匹配项,而不仅仅是首次匹配项。例如查找匹配表达式的所有项。
fmt.Println(r.FindAllString("peach punch pinch", -1))
//All 同样可以对应到上面的所有函数。
fmt.Println(r.FindAllStringSubmatchIndex(
"peach punch pinch", -1))
//这个函数提供一个正整数来限制匹配次数。
fmt.Println(r.FindAllString("peach punch pinch", 2))
//上面的例子中,我们使用了字符串作为参数,并使用了如 MatchString 这样的方法。我们也可以提供 []byte参数并将 String 从函数命中去掉。
fmt.Println(r.Match([]byte("peach")))
//创建正则表示式常量时,可以使用 Compile 的变体MustCompile 。因为 Compile 返回两个值,不能用语常量。
r = regexp.MustCompile("p([a-z]+)ch")
fmt.Println(r)
//regexp 包也可以用来替换部分字符串为其他值。
fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))
//Func 变量允许传递匹配内容到一个给定的函数中,
in := []byte("a peach")
out := r.ReplaceAllFunc(in, bytes.ToUpper)
fmt.Println(string(out))
}
返回结果:
true
true
peach
[0 5]
[peach ea]
[0 5 1 3]
[peach punch pinch]
[[0 5 1 3] [6 11 7 9] [12 17 13 15]]
[peach punch]
true
p([a-z]+)ch
a <fruit>
a PEACH
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述对大家Go语言程序设计有所帮助。
标签:Go语言,正则表达式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用Python复制文件的9种方法总结
2022-06-25 15:00:27
![](https://img.aspxhome.com/file/2023/6/70656_0s.png)
windows mysql 自动备份的几种方法汇总
2024-01-25 05:44:43
python 根据正则表达式提取指定的内容实例详解
2023-07-18 01:23:48
SQL SERVER 2014 安装图解教程(含SQL SERVER 2014下载)
2024-01-16 05:16:07
![](https://img.aspxhome.com/file/2023/1/113591_0s.png)
Python程序运行原理图文解析
2023-08-09 03:27:31
![](https://img.aspxhome.com/file/2023/0/61740_0s.jpg)
YUI Compressor 组件压缩 JavaScript 的一些原理
2009-08-02 20:22:00
mysql执行时间为负数的原因分析
2024-01-19 12:21:20
pyenv与virtualenv安装实现python多版本多项目管理
2022-12-19 23:50:04
![](https://img.aspxhome.com/file/2023/8/112108_0s.png)
Pytorch深度学习addmm()和addmm_()函数用法解析
2021-01-02 04:04:25
![](https://img.aspxhome.com/file/2023/2/95252_0s.gif)
如何在vue项目中使用UEditor--plus
2024-05-10 14:20:43
![](https://img.aspxhome.com/file/2023/5/126385_0s.jpg)
ASP程序中输出Excel文件实例一则
2008-11-07 15:29:00
在Python中使用成员运算符的示例
2023-12-12 01:36:20
![](https://img.aspxhome.com/file/2023/2/133672_0s.jpg)
Go语言读写锁RWMutex的源码分析
2024-05-09 09:47:55
![](https://img.aspxhome.com/file/2023/1/127331_0s.jpg)
用 Python 连接 MySQL 的几种方式详解
2023-07-25 08:08:50
![](https://img.aspxhome.com/file/2023/9/61709_0s.jpg)
golang常用库之配置文件解析库-viper使用详解
2024-02-17 18:36:46
![](https://img.aspxhome.com/file/2023/5/107315_0s.png)
python数据类型判断type与isinstance的区别实例解析
2022-07-05 03:25:50
mysql中全连接full join...on...的用法说明
2024-01-13 19:44:22
MySQL之常用的MySQL优化工具解读
2024-01-23 20:15:06
![](https://img.aspxhome.com/file/2023/1/69841_0s.jpg)
微信小程序嵌入腾讯视频源过程详解
2024-04-18 10:10:25
python中快速进行多个字符替换的方法小结
2021-08-19 05:59:34