Situation: J'ai un fichier html et j'ai besoin de supprimer certaines sections.Trouver une chaîne et la remplacer plus efficacement
Par exemple: Le fichier contient html: <div style="padding:10px;">First Name:</div><div style="padding:10px; background-color: gray">random information here</div><div style="padding:10px;">First Name:</div><div style="padding:10px; background-color: gray">random information here</div>
Je dois enlever tout le texte qui commence par "<div style="padding:10px; background-color: gray">
" et se termine par "</div>
" de sorte que le résultat serait:
<div style="padding:10px;">First Name:</div><div style="padding:10px;">First Name:</div>
I créé 2 fonctions qui font cela, mais je ne le fais pas du tout efficace. J'ai un fichier de 40 Mo et il faut environ 2 heures pour terminer le programme. Y a-t-il un moyen plus efficace de le faire? Existe-t-il un moyen d'utiliser regex?
Voir mon code ci-dessous:
Public Shared Function String_RemoveText(ByVal startAt As String, ByVal endAt As String, ByVal SourceString As String) As String
Dim TotalCount As Integer = String_CountCharacters(SourceString, startAt)
Dim CurrentCount As Integer = 0
RemoveNextString:
Dim LeftRemoved As String = Mid(SourceString, InStr(SourceString, startAt) + 1, Len(SourceString) - Len(endAt))
Dim RemoveCore As String = Left(LeftRemoved, InStr(LeftRemoved, endAt) - 1)
Dim RemoveString As String = startAt & RemoveCore & endAt
Do
' Application.DoEvents()
SourceString = Replace(SourceString, RemoveString, "")
If InStr(SourceString, startAt) < 1 Then Exit Do
GoTo RemoveNextString
Loop
Return Replace(SourceString, RemoveString, "")
End Function
Public Shared Sub Files_ReplaceText(ByVal DirectoryPath As String, ByVal SourceFile As String, ByVal DestinationFile As String, ByVal sFind As String, ByVal sReplace As String, ByVal TrimContents As Boolean, ByVal RemoveCharacters As Boolean, ByVal rStart As String, ByVal rEnd As String)
'CREATE NEW FILENAME
Dim DateFileName As String = Date.Now.ToString.Replace(":", "_")
DateFileName = DateFileName.Replace(" ", "_")
DateFileName = DateFileName.Replace("/", "_")
Dim FileExtension As String = ".txt"
Dim NewFileName As String = DirectoryPath & DateFileName & FileExtension
'CHECK IF FILENAME ALREADY EXISTS
Dim counter As Integer = 0
If IO.File.Exists(NewFileName) = True Then
'CREATE NEW FILE NAME
Do
'Application.DoEvents()
counter = counter + 1
If IO.File.Exists(DirectoryPath & DateFileName & "_" & counter & FileExtension) = False Then
NewFileName = DirectoryPath & DateFileName & "_" & counter & FileExtension
Exit Do
End If
Loop
End If
'END NEW FILENAME
'READ SOURCE FILE
Dim sr As New StreamReader(DirectoryPath & SourceFile)
Dim content As String = sr.ReadToEnd()
sr.Close()
'WRITE NEW FILE
Dim sw As New StreamWriter(NewFileName)
'REPLACE VALUES
content = content.Replace(sFind, sReplace)
'REMOVE STRINGS
If RemoveCharacters = True Then content = String_RemoveText(rStart, rEnd, content)
'TRIM
If TrimContents = True Then content = Regex.Replace(content, "[\t]", "")
'WRITE FILE
sw.Write(content)
'CLOSE FILE
sw.Close()
End Sub
Exemple pour exécuter le code (supprime également Chr (13) & Chr (10): Files_ReplaceText(tPath.Text, tSource.Text, "", Chr(13) & Chr(10), "", True, True, tStart.Text, tEnd.Text)
+1 Pour ne pas analyser HTML avec RegEx. Il existe de meilleures solutions qui existent déjà. Jamais essayé HTML Agility Pack, donc je ne peux pas parler de ça. –
+1 pour la démonstration convaincante. Merci :) – sarnold