Skip to content

Instantly share code, notes, and snippets.

@rafgugi
Created November 12, 2018 07:02
Show Gist options
  • Save rafgugi/29a260131df52f27e19f07361c2de3b7 to your computer and use it in GitHub Desktop.
Save rafgugi/29a260131df52f27e19f07361c2de3b7 to your computer and use it in GitHub Desktop.
Parses sequence with the given regular expression and captures the group values defined in the keys.
package util
import (
"regexp"
)
/**
* Parses sequence with the given regular expression and captures the
* group values defined in the keys.
*/
func GetParamsByRegex(regEx, seq string, keys []string) (map[string]string) {
compRegEx := regexp.MustCompile(regEx)
match := compRegEx.FindStringSubmatch(seq)
params := make(map[string]string)
/* ensure the keys.size + 1 and number of captures have the same size.
* otherwise, raise an error */
for i, val := range match {
if i > 0 { // the first index is the sequence itself, ignore it
params[keys[i-1]] = val
}
}
return params
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment