Golang:计算Go中字符串中一个或多个子串的出现次数?

Pig*_*Pig 5 string algorithm go

我知道,为了计算一个子字符串的出现,我可以使用"strings.Count(,)".如果我想计算substring1或substring2的出现次数该怎么办?有没有比使用strings.count()编写另一个新行更优雅的方法?

Jim*_*imB 7

使用正则表达式:

https://play.golang.org/p/xMsHIYKtkQ

aORb := regexp.MustCompile("A|B")

matches := aORb.FindAllStringIndex("A B C B A", -1)
fmt.Println(len(matches))
Run Code Online (Sandbox Code Playgroud)