bitbucket.org/ai69/amoy@v0.2.3/regex.go (about)

     1  package amoy
     2  
     3  import (
     4  	"regexp"
     5  
     6  	ys "github.com/1set/gut/ystring"
     7  )
     8  
     9  // NamedValues represents a named value map for named capturing groups.
    10  type NamedValues map[string]string
    11  
    12  // ExtractNamedValues returns a named value map with the given compiled regular expression and original string.
    13  func ExtractNamedValues(r *regexp.Regexp, str string) NamedValues {
    14  	subMatchMap := make(NamedValues)
    15  	if r == nil {
    16  		return subMatchMap
    17  	}
    18  	if match := r.FindStringSubmatch(str); len(match) > 0 {
    19  		for i, name := range r.SubexpNames() {
    20  			if i != 0 && ys.IsNotEmpty(name) {
    21  				subMatchMap[name] = match[i]
    22  			}
    23  		}
    24  	}
    25  	return subMatchMap
    26  }
    27  
    28  // IsEmpty indicates if the given map is empty.
    29  func (l NamedValues) IsEmpty() bool {
    30  	return len(l) == 0
    31  }
    32  
    33  // RegexMatch reports whether the string s contains any match of the regular expression pattern.
    34  func RegexMatch(pat, s string) (bool, error) {
    35  	return regexp.MatchString(pat, s)
    36  }