How to determine the number of matches

(飞 鱼儿) #1
package main

import (

func main() {
	r := "^(?P<a>x){1,3}(?P<b>123){0,2}(?P<a>x){1,3}$"

	getParams(r, "xxx")

// Regex store complier
var Regex sync.Map

func getParams(regEx, str string) (paramsMap map[string][]string) {
	var compRegEx *regexp.Regexp
	tmp, ok := Regex.Load(regEx)
	if !ok {
		compRegEx = regexp.MustCompile(regEx)
		Regex.Store(regEx, compRegEx)
	} else {
		// panic
		compRegEx, ok = tmp.(*regexp.Regexp)
		if !ok {
			return nil

	match := compRegEx. FindStringSubmatch(str)
	if len(match) == 0 {
		return nil
	return nil

my question is:
how to determine the number of matches of “x” ,the first regix mode “(?Px){1,3}” matched quantity how much?and the second the mode “(?Px){1,3}” get how much?
i dont konw how to determine。who can tell me ?thank you very much

(Jakob Borg) #2

Check the length of the returned matches?

(Justin) #3

Sorry, would like to try to understand your question more better. Are you trying to count the number of times “x” occurred? or did i miss the point