首页
视频
资源
登录
原
go-regex
2994
人阅读
2022/4/27 22:05
总访问:
92715
评论:
0
收藏:
0
手机
分类:
golang
```go package main import ( "fmt" "regexp" ) // 正则表达式 // 1. 匹配字符 // 2. 替换字符 // 3. 查找字符 // https://pkg.go.dev/regexp#section-documentation func main() { fmt.Println("1. 匹配字符") matchFun() fmt.Println("2. 查找字符") replaceFun() fmt.Println("3. 查找字符") compileFun() fmt.Println("4. 其他") compileOtherFun() } // 1. 匹配字符 func matchFun() { //目标源是字节 matched, err := regexp.Match("^abc.*z$", []byte("abcdefghwz")) fmt.Println(matched, err) //目标源是string matched, err = regexp.MatchString("^abc.*z$", "abcdedafgz") fmt.Println(matched, err) } func replaceFun() { re, _ := regexp.Compile("f([a-z]+)") fmt.Println("1. ", string(re.ReplaceAll([]byte("this is foo, that is func "), []byte("x")))) fmt.Println("2. ", string(re.ReplaceAllString("this is foo, that is func ", "x"))) } //查找字符 func compileFun() { re := regexp.MustCompile(".com") fmt.Println("1:" + re.FindString("www.baidu.com")) fmt.Println("2:" + re.FindString("www.wangyi.com")) fmt.Println("3:" + re.FindString("www.baidu.org")) } func compileOtherFun() { re, _ := regexp.Compile("f([a-z]+)") fmt.Println("1. ", re.Match([]byte("foo"))) fmt.Println("2. ", re.MatchString("foo")) //只匹配一次 fmt.Println("3. ", re.FindString("foo, func")) //返回查找到的所有索引 fmt.Println("4. ", re.FindStringIndex("foo, func")) //只匹配一次, 返回的结果中 , 索引为0的值 是整个匹配串的值, 第二个是子表达式的值,如果没有子表达式, 则不检测 fmt.Println("5. ", re.FindStringSubmatch("foo, func")) // n为-1的时候, 匹配所有符合条件的 字符串, n不为-1的时候表示匹配n次 fmt.Println("6. ", re.FindAllString("foo func fan ", -1)) fmt.Println("7. ", re.FindAllString("foo func fan ", 2)) } ```
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
瑾语
无论这个世界对你怎样,都请你一如既往的努力、勇敢、充满希望。
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
小技巧
5篇
Net
4篇
编程基础
2篇
golang
7篇
mq
2篇
概念
1篇
golang并发
0篇
面试经
0篇
go坑
1篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术