如何使用正则表达式匹配任何重复的字符? [英] how do I match a any repeating character using regexp?
本文介绍了如何使用正则表达式匹配任何重复的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要匹配任何重复两次的字符,例如:
abccdeff
匹配cc和ff。在任何其他正则表达式语法中,我们使用Javascript作为一个简单的例子,我可以这样做:
var str =abccdeff;
var r = /([az] {1})\ 1 / g
console.log(str.match(r))
其中返回
['cc','ff']
但Go的正则表达式似乎不允许这样做。是否可以在Go中执行此操作?
解决方案
由于反向引用是不受re2支持,您需要: I need to match any character that's repeated twice, for example:
glenn-brown / golang-pkg-pcre
)
ul> "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})\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?
解决方案
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
这篇关于如何使用正则表达式匹配任何重复的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文