Created
November 12, 2018 07:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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