How to determine the number of matches

package main

import (
	"fmt"
	"regexp"
	"sync"
)

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
	}
	fmt.Println(match)
	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

2 Likes

Check the length of the returned matches?

2 Likes

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

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.