如何在GO语言中检查一个字符串是否有超过n个重复模式?

huangapple go评论83阅读模式
英文:

How to check if a string has a more than n repetitive patterns in GO?

问题

我想检查一个字符串是否包含超过阈值的重复模式。

例如,这两个字符串都超过了阈值2:

"xyzxyzxyz" // 连续出现了3次"xyz"
"abxyxyxyns" // 连续出现了3次"xy"

有人知道这是如何实现的吗?

英文:

I want to check if a string contains repetitive patterns above a threshold .

For example, these two strings both exceed a threshold of 2:

"xyzxyzxyz" // contains "xyz" 3 times in succession
"abxyxyxyns" //  contains "xy" 3 times in succession

Does anyone know how this is possible?

答案1

得分: 1

使用"repetitions"修饰符。

re := regexp.MustCompile((xy){3,}) // 匹配"xy"出现3次或更多次
fmt.Println(re.MatchString("abxyxyns")) // false
fmt.Println(re.MatchString("abxyxyxyns")) // true

关于regexp包的RE2实现的可用选项在此处有文档记录:

https://github.com/google/re2/wiki/Syntax

英文:

Use the "repetitions" modifier.

re := regexp.MustCompile(`(xy){3,}`)      // match "xy" 3 or more times
fmt.Println(re.MatchString("abxyxyns"))   // false
fmt.Println(re.MatchString("abxyxyxyns")) // true

The available options for the regpexp package's RE2 implementation are documented here:

https://github.com/google/re2/wiki/Syntax

huangapple
  • 本文由 发表于 2016年8月22日 21:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/39080652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定