1 Star 1 Fork 0

zheng.cui/go-oauth2-server

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
regex.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
Richard Knop 提交于 2016-09-28 15:00 +08:00 . Big refactor, updates and cleaning up.
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ongo360/go-oauth2-server.git
git@gitee.com:ongo360/go-oauth2-server.git
ongo360
go-oauth2-server
go-oauth2-server
master

搜索帮助