代码拉取完成,页面将自动刷新
package gf
import (
"regexp"
"strings"
"gitee.com/liumou_site/gbm"
)
// grepBase searches for strings that match a regular expression pattern in a given text.
// This function takes two parameters: text represents the text to be searched, and re represents the regular expression pattern.
// It returns all non-empty matches and possible errors.
func grepBase(text, re string) (res string, err error) {
// Compile the regular expression pattern into a regular expression object.
// If the compilation fails, throw an error directly.
c, err := regexp.Compile(re)
if err != nil {
panic(err)
}
// Split the text by lines for subsequent line-by-line matching.
TxtSp := strings.Split(text, "\n")
// Iterate through each line of text and use the regular expression object to find all matches.
for _, v := range TxtSp {
// Find all matches in the current line and store them in the tmp array.
tmp := c.FindAllString(v, -1)
// If matches are found, append the first match result to the res string.
if len(tmp) > 0 {
res = res + tmp[0] + "\n"
}
}
// Return the collected match results and errors.
return
}
// MatchFirst 尝试在给定文本中匹配指定字符串开头的单词。
// 参数text是搜索的文本,match是需要匹配的字符串。
// 返回匹配到的单词(如果有的话)和可能出现的错误。
// 此函数使用正则表达式来实现匹配,特别地,它匹配以match开始且后面跟随零个或多个非空白字符的单词。
func MatchFirst(text, match string) (res string, err error) {
// 生成Re正则,用于匹配以match开始的单词。
re := match + "[^s]*"
// 调用grepBase函数执行实际的正则匹配。
// grepBase函数是执行正则表达式匹配的核心函数。
// 它接受待搜索的文本和正则表达式作为参数,并返回匹配结果和可能的错误。
return grepBase(text, re)
}
// MatchTail 使用正则表达式在文本中匹配以特定字符串结尾的内容
// 参数:
//
// text - 需要进行匹配的文本
// match - 要匹配的结尾字符串
//
// 返回值:
//
// res - 匹配到的结果字符串
// err - 错误信息,如果匹配过程中发生错误
func MatchTail(text, match string) (res string, err error) {
// 生成Re正则,用于匹配以'match'字符串结尾的内容
re := "[^s]*" + match
// 调用grepBase函数进行实际的匹配处理
return grepBase(text, re)
}
// Match 函数执行模糊匹配。
// 它接受两个字符串参数:text 是待搜索的文本,match 是匹配模式。
// 函数返回一个字符串 res,表示匹配的结果,以及一个错误 err,如果匹配过程中发生错误则不为 nil。
func Match(text, match string) (res string, err error) {
// 生成Re正则,用于模糊匹配。
// 在match前后添加通配符,以实现模糊匹配的效果。
re := "(.*)" + match + "[^s]*"
// 调用grepBase函数执行实际的匹配逻辑。
// grepBase函数的具体实现未在代码中给出,它负责根据提供的文本和正则表达式进行匹配。
return grepBase(text, re)
}
// Grep 通过关键词筛选所有匹配行
// 它通过调用 Match 函数来实现文本的匹配,并根据匹配结果更新 rs.Text 和 rs.Err。
// 之后,它将匹配到的文本按行分割成字符串切片,并移除其中的空行,然后返回 rs。
func (rs *ReadScreen) Grep(match string) *ReadScreen {
// 调用 Match 函数尝试匹配文本,并将结果赋值给 rs.Text 和 rs.Err。
rs.Text, rs.Err = Match(rs.Text, match)
// 将匹配到的文本按行分割成字符串切片。
r := strings.Split(rs.Text, "\n")
// 使用 gbm.SliceRemoveNull 方法移除切片中的空行。
rs.Slice = gbm.SliceRemoveNull(r)
// 返回处理后的 ReadScreen 实例。
return rs
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。