代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。