代码拉取完成,页面将自动刷新
package util
import (
"errors"
"regexp"
)
var (
// ErrNoMatchFound ...
ErrNoMatchFound = errors.New("No match found")
)
// RegexExtractMatches extracts multiple named matches from a string using regex
func RegexExtractMatches(in, regExp string, names ...string) (map[string]string, error) {
compiledRegex, err := regexp.Compile(regExp)
if err != nil {
return map[string]string{}, err
}
subExpNames := compiledRegex.SubexpNames()
results := compiledRegex.FindAllStringSubmatch(in, -1)
if len(results) == 0 {
return map[string]string{}, ErrNoMatchFound
}
matches := make(map[string]string, len(names))
for i, match := range results[0] {
for _, name := range names {
if subExpNames[i] == name {
matches[name] = match
}
}
}
return matches, nil
}
// RegexExtractMatch extracts a named match from a string using regex
func RegexExtractMatch(in, regularExpression, name string) (string, error) {
compiledRegex, err := regexp.Compile(regularExpression)
if err != nil {
return "", err
}
names := compiledRegex.SubexpNames()
results := compiledRegex.FindAllStringSubmatch(in, -1)
if len(results) == 0 {
return "", ErrNoMatchFound
}
for i, match := range results[0] {
if names[i] != name {
continue
}
return match, nil
}
return "", ErrNoMatchFound
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。