regexp.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package model
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "go-common/app/service/main/antispam/util"
  7. )
  8. const (
  9. // OperationLimit .
  10. OperationLimit = "limit"
  11. // OperationRestrictLimit .
  12. OperationRestrictLimit = "restrict"
  13. // OperationPutToWhiteList .
  14. OperationPutToWhiteList = "white"
  15. // OperationIgnore .
  16. OperationIgnore = "ignore"
  17. )
  18. // Regexp .
  19. type Regexp struct {
  20. ID int64 `json:"id"`
  21. Area string `json:"area"`
  22. AdminID int64 `json:"admin_id"`
  23. AdminName string `json:"-"`
  24. Reg *regexp.Regexp `json:"-"`
  25. Name string `json:"name"`
  26. Operation string `json:"op"`
  27. Content string `json:"content"`
  28. State string `json:"state"`
  29. CTime util.JSONTime `json:"-"`
  30. MTime util.JSONTime `json:"mtime"`
  31. }
  32. // FindString .
  33. func (r *Regexp) FindString(content string) string {
  34. if hits := r.Reg.FindStringSubmatch(content); len(hits) >= 2 {
  35. return strings.TrimSpace(hits[1])
  36. }
  37. return ""
  38. }
  39. func (r *Regexp) String() string {
  40. return fmt.Sprintf("name:%s, operation:%s, regexp:%s\n",
  41. r.Name, r.Operation, r.Content)
  42. }