You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matcher.go 658 B

4 years ago
123456789101112131415161718192021222324252627282930313233
  1. package grsync
  2. import (
  3. "regexp"
  4. )
  5. type matcher struct {
  6. regExp *regexp.Regexp
  7. }
  8. func (m matcher) Match(data string) bool {
  9. return m.regExp.Match([]byte(data))
  10. }
  11. func (m matcher) Extract(data string) string {
  12. const submatchCount = 1
  13. matches := m.regExp.FindAllStringSubmatch(data, submatchCount)
  14. if len(matches) == 0 || len(matches[0]) < 2 {
  15. return ""
  16. }
  17. return matches[0][1]
  18. }
  19. func (m matcher) ExtractAllStringSubmatch(data string, submatchCount int) [][]string {
  20. return m.regExp.FindAllStringSubmatch(data, submatchCount)
  21. }
  22. func newMatcher(regExpString string) *matcher {
  23. return &matcher{
  24. regExp: regexp.MustCompile(regExpString),
  25. }
  26. }