2012-01-13 50 views
3

對不起,如果它是一個問題的重複,但這是Vb.Net正則表達式函數的具體內容。如何在VB.Net上使用Regex刪除字符串上的重複字符(超過3次出現)?

我需要刪除給定字符串上出現3個或更多重複字符的情況。例如:

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

Debug.Print(output) 

「????」第一部分我猜應該是正則表達式,我必須假設我幾乎不知道。

我不知道這是否是正確的語法。但我需要輸出來作爲:

「1.2和33」

因此任何方向表示讚賞。

回答

11

這將產生所需的結果:

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

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 

注意,更換$1 - 這是代表​​第一捕獲組的結果的變量。

相關問題