Link to home
Start Free TrialLog in
Avatar of TommyV10
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
Avatar of RonaldBiemans
RonaldBiemans

I don't know about a regular expression but this should do the same

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
Here is a resource of hundreds of already made regexpressions.

http://regexlib.com/DisplayPatterns.aspx
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TommyV10

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
([\w])\1+

Bob