你可以使用正则表达式来匹配任意重复的字符。

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

how do I match a any repeating character using regexp?

问题

我需要匹配任何重复两次的字符,例如:

"abccdeff"

应该匹配 "cc" 和 "ff"。在任何其他正则表达式语法中,让我们以 JavaScript 作为一个快速示例,我可以这样做:

var str = "abccdeff";
var r = /([a-z]{1})/g
console.log(str.match(r))

它返回

[ 'cc', 'ff' ]

但是 Go 的正则表达式似乎不允许这样做。在 Go 中是否有可能实现这个?

英文:

I need to match any character that's repeated twice, for example:

"abccdeff"

Should match "cc" and "ff". In any other regex syntax, let's use Javascript as a quick example, I could do:

var str = "abccdeff";
var r = /([a-z]{1})/g
console.log(str.match(r))

Which returns

[ 'cc', 'ff' ]

But Go's regexp doesn't seem to allow that. Is it possible to do this in Go?

答案1

得分: 5

由于re2不支持反向引用,您需要:

英文:

Since backreference is not supported by re2, you would need:

huangapple
  • 本文由 发表于 2015年12月6日 00:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/34107791.html
匿名

发表评论

匿名网友

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

确定