honeycombio/honeytail · regex.go
go logo
func ParseLineRegexes(regexStrs []string) ([]*regexp.Regexp, error) {
	regexes := make([]*regexp.Regexp, 0)
	for _, regexStr := range regexStrs {
		regex, err := ParseLineRegex(regexStr)
		if err != nil {
			return regexes, err
		}
		regexes = append(regexes, regex)
	}
	return regexes, nil
}
Similar code snippets
1.
honeycombio/honeytail · regex.go
Match rating: 74.23% · See similar code snippets
go logo
func NewRegexLineParser(regexStrs []string) (*RegexLineParser, error) {
	lineRegexes, err := ParseLineRegexes(regexStrs)
	if err != nil {
		return nil, err
	}
	logrus.WithFields(logrus.Fields{
		"lineRegexes": lineRegexes,
	}).Debug("Compiled line regexes")
	return &RegexLineParser{lineRegexes}, nil
}
2.
honeycombio/honeytail · regex.go
Match rating: 64.61% · See similar code snippets
go logo
func ParseLineRegex(regexStr string) (*regexp.Regexp, error) {
	// Regex can't be blank
	if regexStr == "" {
		logrus.Debug("LineRegex is blank; required field")
		return nil, errors.New("Must provide a regex for parsing log lines; use `--regex.line_regex` flag.")
	}

	// Compile regex
	lineRegex, err := regexp.Compile(regexStr)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"lineRegex": regexStr,
		}).Error("Could not compile line regex")
		return nil, err
	}

	// Require at least one named group
	var numNamedGroups int
	for _, groupName := range lineRegex.SubexpNames() {
		if groupName != "" {
			numNamedGroups++
		}
	}
	if numNamedGroups == 0 {
		logrus.WithFields(logrus.Fields{
			"LineRegex": regexStr,
		}).Error("No named capture groups")
		return nil, errors.New(fmt.Sprintf("No named capture groups found in regex: '%s'. Must provide at least one named group with line regex. Example: `(?P<name>re)`", regexStr))
	}

	return lineRegex, nil
}
3.
auroratechnologies/vangoh · vangoh.go
Match rating: 62.85% · See similar code snippets
go logo
func (vg *Vangoh) IncludeHeader(headerRegex string) error {
	var regexBuf bytes.Buffer
	if !strings.HasPrefix(headerRegex, "^") {
		regexBuf.WriteString("^")
	}
	regexBuf.WriteString(headerRegex)
	if !strings.HasSuffix(headerRegex, "$") {
		regexBuf.WriteString("$")
	}

	regex := regexBuf.String()
	compiled, err := regexp.Compile(regex)
	if err != nil {
		return err
	}
	vg.includedHeaders[regex] = compiled
	return nil
}
4.
leanovate/gopter · regex.go
Match rating: 62.19% · See similar code snippets
go logo
func RegexMatch(regexStr string) gopter.Gen {
	regexSyntax, err1 := syntax.Parse(regexStr, syntax.Perl)
	regex, err2 := regexp.Compile(regexStr)
	if err1 != nil || err2 != nil {
		return Fail(reflect.TypeOf(""))
	}
	return regexMatchGen(regexSyntax.Simplify()).SuchThat(func(v string) bool {
		return regex.MatchString(v)
	}).WithShrinker(StringShrinker)
}
5.
gocraft/web · tree.go
Match rating: 61.29% · See similar code snippets
go logo
func compileRegexp(regStr string) *regexp.Regexp {
	if regStr == "" {
		return nil
	}

	return regexp.MustCompile("^" + regStr + "$")
}
6.
rockneurotiko/go-tgbot · utils.go
Match rating: 60.77% · See similar code snippets
go logo
func convertToCommand(reg string) string {
	if !strings.HasSuffix(reg, "$") {
		reg = reg + "$"
	}
	if !strings.HasPrefix(reg, "^/") {
		if strings.HasPrefix(reg, "/") {
			reg = "^" + reg
		} else {
			reg = "^/" + reg
		}
	}
	return reg
}
7.
kubernetes/test-infra · blockade.go
Match rating: 60.68% · See similar code snippets
go logo
func matchesAny(str string, regexps []*regexp.Regexp) bool {
	for _, reg := range regexps {
		if reg.MatchString(str) {
			return true
		}
	}
	return false
}
8.
yukimemi/core · core.go
Match rating: 60.44% · See similar code snippets
go logo
func CompileStrs(regStrs []string) (*regexp.Regexp, error) {
	if len(regStrs) == 0 {
		return nil, fmt.Errorf("regStrs must be greater than or equal to 1")
	}
	regStr := "(" + strings.Join(regStrs, "|") + ")"
	re, err := regexp.Compile(regStr)
	if err != nil {
		return nil, err
	}
	return re, nil
}
9.
matryer/silk · line.go
Match rating: 59.93% · See similar code snippets
go logo
func init() {
	// compile regexes
	regexes = make(map[string]*regexp.Regexp, len(matchTypes))
	for _, item := range matchTypes {
		regexes[item.R] = regexp.MustCompile(item.R)
	}
}
10.
abhishekkr/gol · grep.go
Match rating: 59.44% · See similar code snippets
go logo
func Grep(re, lines string) (string, error) {
	match := ""
	regex, regexErr := regexp.Compile(re)
	if regexErr != nil {
		return "", regexErr
	}

	for _, line := range strings.Split(lines, "\n") {
		if regex.MatchString(line) {
			match = match + "\n" + line
		}
	}
	return match, nil
}