regex_match.go 556 B

1234567891011121314151617181920212223242526272829303132
  1. package match
  2. import (
  3. log "github.com/sirupsen/logrus"
  4. "pmail/dto/parsemail"
  5. "pmail/utils/context"
  6. "regexp"
  7. )
  8. type RegexMatch struct {
  9. Rule string
  10. Field string
  11. }
  12. func NewRegexMatch(field, rule string) *RegexMatch {
  13. return &RegexMatch{
  14. Rule: rule,
  15. Field: field,
  16. }
  17. }
  18. func (r *RegexMatch) Match(ctx *context.Context, email *parsemail.Email) bool {
  19. content := getFieldContent(r.Field, email)
  20. match, err := regexp.MatchString(r.Rule, content)
  21. if err != nil {
  22. log.WithContext(ctx).Errorf("rule regex error %v", err)
  23. }
  24. return match
  25. }