首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// 通用邮箱正则
|
||||
var emailRegex = regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`)
|
||||
|
||||
func main() {
|
||||
// 测试文本:复用文档本地测试域名,零外网依赖
|
||||
content := "工作邮箱:demo@miaoall.cn,备用邮箱:admin@miaoall.cn,网站地址:https://www.miaoall.cn"
|
||||
contentBytes := []byte(content) // 字节流数据,适配[]byte系列API
|
||||
|
||||
fmt.Println("========== 1. 基础匹配校验API ==========")
|
||||
// 字符串匹配校验
|
||||
fmt.Println("字符串是否含邮箱(MatchString):", emailRegex.MatchString(content))
|
||||
// 字节流匹配校验
|
||||
fmt.Println("字节流是否含邮箱(Match):", emailRegex.Match(contentBytes))
|
||||
|
||||
fmt.Println("\n========== 2. 单次匹配内容/下标API ==========")
|
||||
// 单次字符串匹配内容
|
||||
firstEmail := emailRegex.FindString(content)
|
||||
fmt.Println("首个匹配邮箱(FindString):", firstEmail)
|
||||
// 单次字符串匹配下标
|
||||
firstIndex := emailRegex.FindStringIndex(content)
|
||||
fmt.Println("首个邮箱起止下标(FindStringIndex):", firstIndex)
|
||||
// 单次字节流匹配内容、下标
|
||||
firstByteEmail := emailRegex.Find(contentBytes)
|
||||
fmt.Println("字节流首个邮箱(Find):", string(firstByteEmail))
|
||||
firstByteIndex := emailRegex.FindIndex(contentBytes)
|
||||
fmt.Println("字节流首个邮箱下标(FindIndex):", firstByteIndex)
|
||||
|
||||
fmt.Println("\n========== 3. 批量匹配内容/下标API ==========")
|
||||
// 批量匹配所有字符串结果
|
||||
allEmail := emailRegex.FindAllString(content, -1)
|
||||
fmt.Println("全部邮箱(FindAllString):", allEmail)
|
||||
// 批量匹配所有下标
|
||||
allIndex := emailRegex.FindAllStringIndex(content, -1)
|
||||
fmt.Println("全部邮箱下标(FindAllStringIndex):", allIndex)
|
||||
// 字节流批量匹配
|
||||
allByteEmail := emailRegex.FindAll(contentBytes, -1)
|
||||
fmt.Print("字节流全部邮箱(FindAll):")
|
||||
for _, v := range allByteEmail {
|
||||
fmt.Print(string(v), " ")
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("\n========== 4. 基础捕获组匹配API ==========")
|
||||
// 复用链接正则,测试子匹配
|
||||
var urlRegex = regexp.MustCompile(`(?:http|https)://(\w+\.\w+\.\w+)`)
|
||||
// 单次字符串子匹配
|
||||
subMatch := urlRegex.FindStringSubmatch(content)
|
||||
fmt.Println("单次子匹配结果(FindStringSubmatch):", subMatch)
|
||||
// 单次子匹配下标
|
||||
subMatchIndex := urlRegex.FindStringSubmatchIndex(content)
|
||||
fmt.Println("单次子匹配下标(FindStringSubmatchIndex):", subMatchIndex)
|
||||
// 批量子匹配、下标
|
||||
allSubMatch := urlRegex.FindAllStringSubmatch(content, -1)
|
||||
fmt.Println("批量子匹配(FindAllStringSubmatch):", allSubMatch)
|
||||
allSubIndex := urlRegex.FindAllStringSubmatchIndex(content, -1)
|
||||
fmt.Println("批量子匹配下标(FindAllStringSubmatchIndex):", allSubIndex)
|
||||
|
||||
fmt.Println("\n========== 5. 文本分割API ==========")
|
||||
// 按邮箱分割原文本
|
||||
splitRes := emailRegex.Split(content, -1)
|
||||
fmt.Println("正则分割文本结果(Split):", splitRes)
|
||||
|
||||
fmt.Println("\n========== 6. 流式读取API(防OOM) ==========")
|
||||
// 流式读取适配超大文件,逐符文读取,杜绝全量加载内存溢出
|
||||
file, err := os.Open("regexp/app.log")
|
||||
if err != nil {
|
||||
fmt.Println("流式文件读取模拟(无文件则跳过):", err)
|
||||
} else {
|
||||
defer file.Close()
|
||||
// bufio.Reader 实现 io.RuneReader 接口,可用于所有流式正则API
|
||||
reader := bufio.NewReader(file)
|
||||
fmt.Println("流式匹配是否含邮箱(MatchReader):", emailRegex.MatchReader(reader))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user