Exemple #1
0
func RegexpFind(env *Glisp, name string,
	args []Sexp) (Sexp, error) {
	if len(args) != 2 {
		return SexpNull, WrongNargs
	}
	var haystack string
	switch t := args[1].(type) {
	case SexpStr:
		haystack = string(t)
	default:
		return SexpNull,
			errors.New(fmt.Sprintf("2nd argument of %v should be a string", name))
	}

	var needle regexp.Regexp
	switch t := args[0].(type) {
	case SexpRegexp:
		needle = regexp.Regexp(t)
	default:
		return SexpNull,
			errors.New(fmt.Sprintf("1st argument of %v should be a compiled regular expression", name))
	}

	switch name {
	case "regexp-find":
		str := needle.FindString(haystack)
		return SexpStr(str), nil
	case "regexp-find-index":
		return regexpFindIndex(needle, haystack)
	case "regexp-match":
		matches := needle.MatchString(haystack)
		return SexpBool(matches), nil
	}

	return SexpNull, errors.New("unknown function")
}
Exemple #2
0
func (re SexpRegexp) SexpString() string {
	r := regexp.Regexp(re)
	return fmt.Sprintf(`(regexp-compile "%v")`, r.String())
}
func (filter *RegexFilter) Filter(toCompare IPFilePair) bool {
	regex := regexp.Regexp(*filter)
	return (&regex).MatchString(string(toCompare.FileName))
}