package main
import (
"fmt"
"regexp"
)
func main() {
fmt.Println("start")
//传入byte 返回byte
//简单的查找
str := "abcd123ha1231adaf231"
reg := regexp.MustCompile("\\d+")
data := reg.Find([]byte(str))
fmt.Println(string(data))
//index
index := reg.FindIndex([]byte(str))
fmt.Println(index[0], index[1])
//byte查找所有
reg = regexp.MustCompile("\\d+")
dataS := reg.FindAll([]byte(str), -1)
for _, v := range dataS {
fmt.Println(string(v))
}
indexS := reg.FindAllIndex([]byte(str), -1)
for _, v := range indexS {
fmt.Println(v[0], v[1], []byte(str)[v[0]:v[1]])
}
//find string
fmt.Println(reg.FindString(str))
fmt.Println(reg.FindAllString(str, -1))
//find 汉字
str = "he中国go中"
reg = regexp.MustCompile("[\\p{Han}]+")
fmt.Println(reg.FindAllString(str, -1))
//查找并替换
str = "你@的@样子"
reg = regexp.MustCompile("@")
fmt.Println(str, reg.ReplaceAllString(str, "#"))
}
golang的正则操作
最新推荐文章于 2023-10-06 21:20:15 发布