首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 场景1:Longest 长短匹配对比(最核心模式切换)
|
||||
fmt.Println("===== 1. Longest最长匹配 vs 默认first匹配 =====")
|
||||
expr := `(a+)|(aaa)`
|
||||
// 默认 leftmost-first
|
||||
reDefault := regexp.MustCompile(expr)
|
||||
fmt.Println("默认模式匹配aaaa:", reDefault.FindString("aaaa")) // 优先第一个分支a+,短匹配
|
||||
|
||||
// 开启最长匹配
|
||||
reLong := regexp.MustCompile(expr)
|
||||
reLong.Longest()
|
||||
fmt.Println("Longest最长模式匹配aaaa:", reLong.FindString("aaaa"))
|
||||
|
||||
// 场景2:. 匹配换行 RE2专属 (?s:.)
|
||||
fmt.Println("\n===== 2. 点号匹配换行符 =====")
|
||||
strWithLine := "hello\nworld"
|
||||
// 普通. 不跨换行
|
||||
reDotNormal := regexp.MustCompile(`hello.world`)
|
||||
fmt.Println("普通.匹配换行:", reDotNormal.MatchString(strWithLine))
|
||||
// (?s:.) 让.识别换行
|
||||
reDotAll := regexp.MustCompile(`(?s:hello.world)`)
|
||||
fmt.Println("(?s:.)开启跨行匹配:", reDotAll.MatchString(strWithLine))
|
||||
|
||||
// 场景3:大小写不敏感匹配(无修饰符,字符组兼容大小写)
|
||||
fmt.Println("\n===== 3. 大小写忽略匹配方案 =====")
|
||||
target := "Apple APPLE apple aPpLe"
|
||||
// 写法1:字符组罗列大小写
|
||||
reCase := regexp.MustCompile(`[Aa][Pp][Pp][Ll][Ee]`)
|
||||
fmt.Println("大小写全部匹配结果:", reCase.FindAllString(target, -1))
|
||||
|
||||
// 场景4:CompilePOSIX 天生Longest语义
|
||||
fmt.Println("\n===== 4. CompilePOSIX 原生最长匹配 =====")
|
||||
rePosix, _ := regexp.CompilePOSIX(expr)
|
||||
fmt.Println("CompilePOSIX匹配aaaa:", rePosix.FindString("aaaa"))
|
||||
}
|
||||
Reference in New Issue
Block a user