如何在VB.Net上使用Regex删除字符串上的重复字符(超过3次出现)?

Pau*_*eno 3 .net c# regex vb.net

如果它是某个问题的副本,我很抱歉,但这是针对Vb.Net Regex功能的.

我需要删除给定字符串上出现的3个或更多重复字符.例如:

Dim strTmp As String = "111111.......222222 and 33"
Dim output As String = Regex.Replace(strTmp, "????", "")

Debug.Print(output)
Run Code Online (Sandbox Code Playgroud)

"????" 部分我想应该是正则表达式,我必须假设我几乎一无所知.

我不知道这是否是正确的语法.但我需要输出来:

"1.2和33"

所以任何方向都值得赞赏.

Ode*_*ded 11

这将产生所需的结果:

Dim output As String = Regex.Replace("111111.......222222 and 33", 
                                     @"(.)\1{2,}", 
                                     "$1")
Run Code Online (Sandbox Code Playgroud)

output将包含"1.2 and 33".

分解:

(.)   - Match any single character and put in a capturing group
\1    - Match the captured character
{2,}  - Two or more times
Run Code Online (Sandbox Code Playgroud)

请注意,替换是$1- 这是一个代表第一个捕获组的结果的变量.