正则表达式匹配重复的字符 [英] Regex to match repeated characters

查看:517
本文介绍了正则表达式匹配重复的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个匹配字符串的正则表达式,如果它在一行中有3个或更多的重复字符(例如aaaaaa,testtttttt,otttttter)。

I am trying to create a regex that matches a string if it has a 3 or more repetitive characters in a row (e.g. aaaaaa, testtttttt, otttttter).

我试过以下内容:

I have tried the following:

regexp.Compile("[A-Za-z0-9]{3,}")
regexp.Compile("(.){3,}")
regexp.Compile("(.)\\1{3,}")

与某行中的任何 3个字符匹配,但不是连续的字符...我在哪里出错了?

which matches any 3 characters in a row, but not consecutive characters... Where am I going wrong?

推荐答案

要求不能用 true 完成正则表达式,你需要的是(不规则)反向引用。虽然很多正则表达式引擎都实现了它们,但Go使用的RE2并不是。 RE2是一个快速的正则表达式引擎,可以保证线性时间字符串处理,但是还没有已知的方式来实现这种效率的反向引用。 (有关更多信息,请参阅 https://swtch.com/~rsc/regexp/ 。)

What you're asking for cannot be done with true regular expressions, what you need are (irregular) backreferences. While many regexp engines implement them, RE2 used by Go does not. RE2 is a fast regexp engine that guarantees linear time string processing, but there's no known way to implement backreferences with such efficiency. (See https://swtch.com/~rsc/regexp/ for further information.)

要解决您的问题,您可能需要搜索其他正则表达式库。我相信可以找到PCRE的绑定,但我没有亲自体验过。

To solve your problem you may want to search for some other regexp library. I believe bindings for PCRE can be found, but I've no personal experience from them.

另一种方法是手动解析字符串,而不使用(ir)正则表达式。

Another approach would be to parse the string manually without using (ir)regular expressions.

这篇关于正则表达式匹配重复的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆