package _case
import (
"fmt"
"regexp"
)
func RegexpCase() {
test1()
fmt.Println("------------")
test2()
}
func test2() {
// 案例
pattern := `^[a-z]+\[[0-9]+\]$`
re := regexp.MustCompile(pattern)
fmt.Println(re.MatchString("asdf[22114]"))
// FindAll传入[]byte;
// FindAllString传入string
bytes := re.FindAll([]byte("haha[1234]"), -1)
fmt.Println(string(bytes[0]))
re = regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})`)
str := "今天日期:2024-10-21"
replaced := re.ReplaceAllString(str, "我日你妈的")
fmt.Println(replaced)
}
// 常见的正则表达式字符及其含义
// 锚点字符:
//
// ^:匹配字符串的开始。例如,^abc 匹配以 "abc" 开头的字符串。
// $:匹配字符串的结束。例如,abc$ 匹配以 "abc" 结尾的字符串。
// 字符匹配:
//
// .(点号):匹配任意一个字符,除了换行符。例子:a.b 可以匹配 "a_b"、"a1b",但不能匹配 "ab"。
// [ ]:定义字符集合,匹配集合中的任意一个字符。例如,[abc] 匹配 "a"、"b" 或 "c"。
// [^ ]:排除字符集合,匹配集合外的任意字符。例如,[^abc] 匹配除 "a"、"b" 和 "c" 以外的任意字符。
// 量词:
//
// *:匹配前面的元素 0 次或多次。例如,a* 匹配空字符串、"a"、"aa" 等。
// +:匹配前面的元素 1 次或多次。例如,a+ 匹配 "a"、"aa"、"aaa" 等。
// ?:匹配前面的元素 0 次或 1 次。例如,a? 匹配空字符串或 "a"。
// {n}:匹配前面的元素恰好 n 次。例如,a{3} 匹配 "aaa"。
// {n,}:匹配前面的元素至少 n 次。例如,a{2,} 匹配 "aa"、"aaa"、"aaaa" 等。
// {n,m}:匹配前面的元素至少 n 次,但不超过 m 次。例如,a{2,4} 匹配 "aa"、"aaa" 或 "aaaa"。
// 字符类:
//
// \d:匹配任意数字,等同于 [0-9]。
// \D:匹配非数字字符,等同于 [^0-9]。
// \w:匹配字母、数字或下划线,等同于 [a-zA-Z0-9_]。
// \W:匹配非字母、数字或下划线字符,等同于 [^a-zA-Z0-9_]。
// \s:匹配任意空白字符(包括空格、制表符、换行符等)。
// \S:匹配任意非空白字符。
// 分组和选择:
//
// ( ):定义捕获组,匹配的内容可以在后续操作中引用。例如,(abc) 匹配 "abc"。
// |:逻辑或,表示左右两边任意一个匹配即可。例如,a|b 匹配 "a" 或 "b"。
// 反斜杠转义:
//
// \:反斜杠用于转义特殊字符。例如,\. 匹配字符 ".",而不是表示任意字符的点号。
// 特殊字符类:
//
// \b:匹配单词边界。例如,\bword\b 匹配完整的 "word",但不会匹配 "sword" 中的 "word"。
// \B:匹配非单词边界。
// 举例说明
// ^\d{3}-\d{2}-\d{4}$:匹配符合格式的社会保障号(比如 "123-45-6789")。
//
// ^:匹配字符串的开始。
// \d{3}:匹配 3 个数字。
// -:匹配字符 -。
// \d{2}:匹配 2 个数字。
// \d{4}:匹配 4 个数字。
// $:匹配字符串的结束。
// ([A-Z][a-z]+):匹配以大写字母开头的单词(如 "Apple")。
//
// [A-Z]:匹配一个大写字母。
// [a-z]+:匹配一个或多个小写字母。
// ( ):定义捕获组。
func test1() {
// Compile解析并返回一个正则表达式。如果成功返回,该Regexp就可用于匹配文本。
re := regexp.MustCompile(".com")
// FindString
fmt.Println(re.FindString("(cainiao.com)"))
fmt.Println(re.FindString("(cainiao.dz)"))
fmt.Println(re.FindString("(cainiao1.com1)"))
// FindStringIndex return符合正则表达式的字符的索引,左闭右开
fmt.Println(re.FindStringIndex("google.com"))
fmt.Println(re.FindStringIndex("abc.org"))
fmt.Println(re.FindStringIndex("fb.com"))
// FindStringSubmatch 该方法返回具有最左边匹配项和匹配组的字符串。找不到返回空字符串
// ([a-z]+)称为匹配组
re1 := regexp.MustCompile("f([a-z]+)ing")
re2 := regexp.MustCompile("f([a-z]+)ing")
fmt.Println(re1.FindStringSubmatch("flying1"))
fmt.Println(re1.FindStringSubmatch("abcfloatingxyz"))
fmt.Println(re2.FindStringSubmatch("flying1"))
fmt.Println(re2.FindStringSubmatch("abcfloatingxyz"))
fmt.Println("-----------")
pattern := "([a-z]+)"
regex := regexp.MustCompile(pattern)
// FindStringSubmatch 只会返回一个 包含的匹配结果,和一个 匹配组 合成的一个切片
// len:2
matches := regex.FindStringSubmatch("apple banana orange")
fmt.Println(matches[0])
fmt.Println(matches[1])
//fmt.Println(matches[2])
//fmt.Println(matches[3])
// 如果想要匹配所有符合正则表达式的值,要用FindAllStringSubmatch
// n为-1时,匹配所有符合条件的字符串,n不为-1时,表示只匹配n次
// 会返回一个二维切片
matches1 := regex.FindAllStringSubmatch("apple banana orange", -1)
fmt.Println(matches1)
// 遍历匹配结果
for i, match := range matches1 {
fmt.Printf("匹配 %d 结果:%s\n", i, match[0])
fmt.Printf("捕获组 %d 结果:%s\n", i, match[1])
}
// 命名捕获组语法
// (?P<name>pattern)
// name:捕获组名称,pattern:捕获组模式
pattern = "(?P<fruit>[a-z]+)"
// 建立正则表达式
re3 := regexp.MustCompile(pattern)
// 匹配单个值
matches = re3.FindStringSubmatch("apple banana orange")
fruit := matches[re3.SubexpIndex("fruit")]
fmt.Println(fruit)
// 匹配多个值
matches1 = re3.FindAllStringSubmatch("apple banana orange", -1)
for i, match := range matches1 {
fruit = match[re3.SubexpIndex("fruit")]
fmt.Printf("匹配结果%d:%s\n", i, match[0])
fmt.Printf("捕获组组%d:%s\n", i, fruit)
}
// regexp.Match() 判断在[]byte中能否找到给定正则表达式的值,返回bool和err
// .*:表示任意数量的任意字符(包括 0 个字符),可以匹配任何字符,除了换行符
// "^abc.*z$"含义:
// 匹配以abc开头中间任意个数任意类型字符(除换行符外)以z结尾的字符串
matched, err := regexp.Match("^abc.*z$", []byte("abcdeeffgz"))
fmt.Println(matched, err)
matched, err = regexp.Match("^abc.*z$", []byte("bcdefgz"))
fmt.Println(matched, err)
// regexp.MatchString()类似regexp.Match()不过他只传字符串,而Match只传[]byte
}
|