Please let me how to remove double spaces and characters from below string.
Test----$$$$19****[email protected]@@@ Nothing
Clean String =
Test-$19*[email protected] Nothing
I have used regex
"\s+" but it just removing the double spaces and I have tried other patterns of regex but it is too complex... please help me.
I am using vb.net
For a faster alternative try:
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())
What you'll want to do is create a backreference to any character, and then remove the following characters that match that backreference. It's usually possible using the pattern
(.)\1+, which should be replaced with just that backreference (once). It depends on the programming language how it's exactly done.
Dim text As String = "[email protected]@@_&aa&&&" Dim result As String = New Regex("(.)\1+").Replace(text, "$1")
result will now contain
[email protected]_&a&. Alternatively, you can use a lookaround to not remove that backreference in the first place:
Dim text As String = "[email protected]@@_&aa&&&" Dim result As String = New Regex("(?<=(.))\1+").Replace(text, "")
Edit: included examples