請讓我如何從下面的字符串中刪除雙空格和字符。如何從字符串中刪除雙字符和空格
字符串= Test----$$$$19****[email protected]@@@ Nothing
清潔字符串= Test-$19*[email protected] Nothing
我用正則表達式"\s+"
但它只是消除雙重空間,我已經試過正則表達式的其他模式,但它太複雜了......請幫我。
我使用vb.net
請讓我如何從下面的字符串中刪除雙空格和字符。如何從字符串中刪除雙字符和空格
字符串= Test----$$$$19****[email protected]@@@ Nothing
清潔字符串= Test-$19*[email protected] Nothing
我用正則表達式"\s+"
但它只是消除雙重空間,我已經試過正則表達式的其他模式,但它太複雜了......請幫我。
我使用vb.net
對於一個更快的替代方案嘗試:
Dim text As String = "[email protected]@@_&aa&&&"
Dim sb As New StringBuilder(text.Length)
Dim lastChar As Char
For Each c As Char In text
If c <> lastChar Then
sb.Append(c)
lastChar = c
End If
Next
Console.WriteLine(sb.ToString())
謝謝我已經完成了對大數據表的不同列的10-15性能測試,總是發現字符串函數比RegEx更快......似乎我必須使用字符串函數以更好的方式將我的目標歸檔。請給我你的建議。再次感謝。 – Smith
什麼你想要做的就是創建一個反向引用任何字符,然後刪除匹配的反向引用,下面的字符。通常可以使用(.)\1+
這種模式,只應該用反向引用(一次)替換。它取決於編程語言是如何完成的。
Dim text As String = "[email protected]@@_&aa&&&"
Dim result As String = New Regex("(.)\1+").Replace(text, "$1")
result
現在將包含[email protected]_&a&
。另外,您也可以使用環視不刪除反向引用擺在首位:
Dim text As String = "[email protected]@@_&aa&&&"
Dim result As String = New Regex("(?<=(.))\1+").Replace(text, "")
編輯:包括實例
感謝Patrickdev ...我使用vb.net,不知道任何有關RegEx的信息,我正在努力學習它......如果你給我一個示例模式,它將對我有很大的幫助。提前致謝。 – Smith
@Ashi:我已經包含了一些示例,說明如何在VB.NET中執行此操作。 – Patrickdev
非常感謝Patrickdev ......但是它刪除了所有不想刪除alpha字符的雙字符。我只是想刪除$, - ,@,*,等等。對不起,我沒有正確發佈我的問題。 – Smith
這裏是唯一一家以替換所有的多個非字字符一個perl方式:
my $String = 'Test----$$$$19****[email protected]@@@ Nothing';
$String =~ s/(\W)\1+/$1/g;
print $String;
輸出:
Test-$19*[email protected] Nothing
下面是它看起來如何在Java中...
String raw = "Test----$$$$19****[email protected]@@@ Nothing";
String cleaned = raw.replaceAll("(.)\\1+", "$1");
System.out.println(raw);
System.out.println(cleaned);
打印
Test----$$$$19****[email protected]@@@ Nothing
Test-$19*[email protected] Nothing
在哪種語言,你的編程?並非所有的正則表達式引擎都是平等創建的。 – Johnsyweb
在這裏你可以找到一個網站[http://stackoverflow.com/questions/7780794/javascript-regex-remove-duplicate-characters](http://stackoverflow.com/questions/7780794/javascript-regex-remove-duplicate-字符) –
我使用vb.net和不知道任何有關正則表達式,我試圖學習它...如果你給我一個示例模式,這將是對我很大的幫助。提前致謝。 – Smith