Golang: String Match List of Regexps

By Xah Lee. Date: . Last updated: .

Here's a function to check if a string matches any regexes in a list.

package main

import "fmt"
import "regexp"

// stringMatchAnyRegex return true if ss is matched by any regexes.
// version 2018-09-01
func stringMatchAnyRegex(ss string, regexes []string) bool {
	for _, re := range regexes {
		result, err := regexp.MatchString(re, ss)
		if err != nil {
			panic(err)
		}
		if result {
			return true
		}
	}
	return false
}

func main() {

	var bigStr = "xxto"

	var regexList = []string{
		`^\.git$`,
		`^xx`,
	}

	fmt.Printf("%v\n", stringMatchAnyRegex(bigStr, regexList))

}

If you have a question, put $5 at patreon and message me.

Golang

Examples

Reference