Golang正则表达式参考中的MustCompile(查找重复字符)

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

Golang Regexp Reference within MustCompile (Find repeating character)

问题

我在处理Go的正则表达式时遇到了困难。它似乎与其他语言不同,有人可以帮我吗?

目标:我想使用MustCompile在字符串中找到所有重复的字符。
例如:在字符串"APPLE"中,P是重复的。

我尝试了以下代码,但完全没有起作用:

re := regexp.MustCompile("(\w)\${1}\+")

基本上,我想要的是:

([A-Za-z])\1+

有人可以告诉我我做错了什么吗?

以下是示例代码:

https://play.golang.org/p/DeuaIva968

英文:

I am having a hard time with Go's regex. It seems it's different than other language, can someone help me on this.

Obj. I want MustCompile to find all repeated characters in the string.
APPLE (where P's repeating)

re := regexp.MustCompile("(\\w)\${1}\\+")

Above is what I have tried but didn't work at all. Basically I wanted to do:

([A-Za-z])+

Can someone tell me what I am doing wrong?

Example below:

https://play.golang.org/p/DeuaIva968

答案1

得分: 2

显然,由于效率问题,Golang不支持回溯引用。:(
谢谢大家的帮助。

英文:

Apparently Golang doesn't supposed back referencing due to efficiency. Golang正则表达式参考中的MustCompile(查找重复字符)
Thank you everyone for your help.

huangapple
  • 本文由 发表于 2016年4月10日 15:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/36526889.html
匿名

发表评论

匿名网友

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

确定