regex_match.go 639 B

1234567891011121314151617181920212223242526272829303132
  1. package match
  2. import (
  3. "github.com/Jinnrry/pmail/dto/parsemail"
  4. "github.com/Jinnrry/pmail/utils/context"
  5. "github.com/dlclark/regexp2"
  6. log "github.com/sirupsen/logrus"
  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. re := regexp2.MustCompile(r.Rule, 0)
  21. match, err := re.MatchString(content)
  22. if err != nil {
  23. log.WithContext(ctx).Errorf("rule regex error %v", err)
  24. }
  25. return match
  26. }