TommyV10
asked on
Regular expressions
Is it possible in VB.Net to write a regular expression that will replace any character that appears more than once consecutively with just one of that character?
For example 'sssdftrgggh' -> 'sdftrgh' or 11125644766 -> 1256476
Thanks,
Tommy
For example 'sssdftrgggh' -> 'sdftrgh' or 11125644766 -> 1256476
Thanks,
Tommy
Here is a resource of hundreds of already made regexpressions.
http://regexlib.com/DisplayPatterns.aspx
http://regexlib.com/DisplayPatterns.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Bob, that sort of works but it only seems to perfrom the replace if an odd number of characters are encountered. It will replace 'a' or 'aaa' to 'a' but leaves out 'aa' or 'aaaa'
Yeah, I noticed that too. You could try a While...End While loop, until their are no more matches.
Bob
Bob
([\w])\1+
Bob
Bob
Public Function removedbl(ByVal s As String) As String
Dim returnstring As String
Dim x As String = ""
For y As Integer = 0 To s.Length - 1
If Not s.Substring(y, 1) = x Then
returnstring += s.Substring(y, 1)
x = s.Substring(y, 1)
End If
Next
Return (returnstring)
End Function