Ai
1 Star 0 Fork 0

余济舟/aid

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
regexp.go 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
package regexp
import (
"regexp"
"slices"
)
type Regexp struct {
target string
targets []string
re regexp.Regexp
}
var RegexpApp Regexp
func (Regexp) New(original string, attrs ...Attributer) Regexp {
return Regexp{re: *regexp.MustCompile(original)}.Set(attrs...)
}
func (my Regexp) Set(attrs ...Attributer) Regexp {
if len(attrs) > 0 {
for idx := range attrs {
attrs[idx].Register(&my)
}
}
return my
}
// MatchFirst 查找第一个匹配项
func (my Regexp) MatchFirst() string {
matched := my.re.FindStringSubmatch(my.target)
if len(matched) > 1 {
return matched[1]
}
return ""
}
// MatchAll 查找所有匹配项
func (my Regexp) MatchAll() []string {
matched := my.re.FindStringSubmatch(my.target)
if len(matched) == 0 {
return nil
}
if len(matched) > 1 {
ret := make([]string, 0, len(matched)-1)
ret = append(ret, matched[1:]...)
return ret
}
return nil
}
// Contains 是否包含匹配项
func (my Regexp) Contains() bool { return my.re.MatchString(my.target) }
// ContainsAny 是否包含任意一个匹配项
func (my Regexp) ContainsAny() bool {
if len(my.targets) == 0 {
return false
}
return slices.ContainsFunc(my.targets, my.re.MatchString)
}
// FindAllStringSubmatch 查找所有匹配项及子匹配项
func (my Regexp) FindAllStringSubmatch(matchIdx int) []string {
var msg []string
matches := my.re.FindAllStringSubmatch(my.target, -1)
for idx := range matches {
if matchIdx == -1 {
msg = append(msg, matches[idx]...)
} else {
msg = append(msg, matches[idx][matchIdx])
}
}
return msg
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jericho-yu/aid.git
git@gitee.com:jericho-yu/aid.git
jericho-yu
aid
aid
v1.45.13

搜索帮助