Question : How to recognize more than 6 similar characters in a file and remove them

Hi, I have a file where I need to look for any character repeating more than 6 times like for example: "AAAAAA" or "BBBBBB" and so on until "ZZZZZZ". The repeating characters may exist any where in the file and I need to see if any exist and remove them from the file.

Thanks

Answer : How to recognize more than 6 similar characters in a file and remove them

I'm still learning Regular Expressions so test this one with a small sample file and check the Immediate window before you uncomment the line that overwrites the original file!

I used these sites to help me develop the RegEx:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx
http://www.regular-expressions.info/

Here's the code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using ofd As New OpenFileDialog
            If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Try
                    Dim txt As String = My.Computer.FileSystem.ReadAllText(ofd.FileName)

                    Debug.Print("Original Text:")
                    Debug.Print("--------------------------------------------------")
                    Debug.Print(txt)
                    Debug.Print("")

                    Dim pattern As String = "(.+)\1{5,}"
                    txt = System.Text.RegularExpressions.Regex.Replace(txt, pattern, "")

                    Debug.Print("Modified Text:")
                    Debug.Print("--------------------------------------------------")
                    Debug.Print(txt)

                    ' Uncomment the line below when you are sure the RegEx is working as desired!
                    ' My.Computer.FileSystem.WriteAllText(ofd.FileName, txt, False)
                Catch ex As Exception
                    MessageBox.Show("File: " & ofd.FileName & vbCrLf & vbCrLf & ex.ToString, "Error Modifying File", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End Using
    End Sub

End Class
Random Solutions  
 
programming4us programming4us