英文:
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不支持反向引用,您需要:
-
要么使用另一个正则表达式库(例如
glenn-brown/golang-pkg-pcre
) -
要么编写一个循环代码,在没有正则表达式的情况下进行分析
英文:
Since backreference is not supported by re2, you would need:
-
either to use another regex library (like
glenn-brown/golang-pkg-pcre
) -
or code a loop which does the analysis without regexp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论