Ejemplo n.º 1
0
func (s *scrape) MapRegex(node xml.Node) (map[string]string, error) {
	if node.IsValid() == false {
		return nil, errors.New("Invalid node")
	}
	m := make(map[string]string, 1)
	inner := node.String()
	for k, v := range ScrapeRegex {
		// remove new line chars
		reg, _ := regexp.CompilePOSIX("\r\n|\r|\n")
		inner = reg.ReplaceAllString(inner, "")
		// get the real data
		reg, _ = regexp.CompilePOSIX(v[0])
		scraped := reg.FindString(inner)
		scraped = reg.ReplaceAllString(scraped, "$1")

		if scraped != "" {
			m[k] = scraped
		}
	}
	// Skip empty and unwanted
	if len(m) > 0 {
		if m[ScrapeMeta[IGNOREEMPTY]] != "" {
			return m, nil
		}
		return nil, nil
	}
	return nil, nil
}