Files
go-caatsm/internal/parsers/pattern.go
T

39 lines
870 B
Go
Raw Normal View History

2024-07-19 20:02:18 +08:00
package parsers
import (
"caatsm/internal/config"
"regexp"
)
var (
bodyTypePattern = regexp.MustCompile(`^\(([A-Z]{3})(.*\n?)+\)$`)
)
2024-07-19 20:02:18 +08:00
func FindPatterns(messageBody string) *config.BodyConfig {
2024-07-19 20:02:18 +08:00
if match := bodyTypePattern.FindStringSubmatch(messageBody); len(match) > 1 {
name := match[1]
patters := config.GetBodyPatterns()
if body, found := patters[name]; found {
return &body
2024-07-19 20:02:18 +08:00
}
}
return nil
}
func ParseBody(messageBody string) map[string]string {
if body := FindPatterns(messageBody); body != nil {
2024-07-19 20:02:18 +08:00
for _, pattern := range body.Patterns {
if matches := pattern.Expression.FindStringSubmatch(messageBody); matches != nil {
result := make(map[string]string)
for i, name := range pattern.Expression.SubexpNames() {
if i != 0 && name != "" {
result[name] = matches[i]
}
}
return result
}
}
}
return nil
}