kejpa Posted September 22, 2005 Posted September 22, 2005 Hi, I have quite a big piece of html that I need to change for clarity, some rows in the table needs high lightning and I thought I'd use som regex for that but from what I understand newlines mock things up... The pattern is.... a multiline string starting with <tr> and ending with </tr> containing PÅMINN or PÅMINN should be replaced with <tr class="highlight"> the complete string up to the ending </tr> ie <tr> <td>first</td> <td>second</td> <td>PÅMINN</td> </tr> should be.... <tr class="highlight"> <td>first</td> <td>second</td> <td>PÅMINN</td> </tr> TIA /Kejpa Quote
JTDPublix Posted September 22, 2005 Posted September 22, 2005 Hi, I have quite a big piece of html that I need to change for clarity, some rows in the table needs high lightning and I thought I'd use som regex for that but from what I understand newlines mock things up... The pattern is.... a multiline string starting with <tr> and ending with </tr> containing PÅMINN or PÅMINN should be replaced with <tr class="highlight"> the complete string up to the ending </tr> ie should be.... TIA /Kejpa Quick and dirty so optimize if you like. I did test the code so I know it works but I haven't done any extensive unit testing ;) Dim sr As StreamReader = File.OpenText("c:\test.html") Dim keyword As String = "PÅMINN" Dim findText As String = "<tr>" Dim rplText As String = "<tr class='highlight'>" If Not IsNothing(sr) Then Dim html As String = sr.ReadToEnd() Dim newHtml As String = String.Empty Dim firstRowPos As Integer = 0 Dim lastRowPos As Integer = 0 Dim rowInstance As String = String.Empty If html <> String.Empty Then Dim pos As Integer = 1 Do While pos < html.Length 'Get row firstRowPos = InStr(pos, html, "<tr>", CompareMethod.Text) lastRowPos = InStr(pos, html, "</tr>", CompareMethod.Text) 'Add for tag length since instr returns first char position If lastRowPos <> 0 Then lastRowPos += Len("</tr>") End If 'If no more instances of a row If firstRowPos = 0 Then 'Get the rest of the file newHtml &= Trim(Mid(html, pos, html.Length - pos)) Exit Do End If 'If file doesn't begin with "<tr>" then grab everything before the first instance of "<tr>" If firstRowPos <> 0 And pos = 1 Then newHtml = Mid(html, 1, firstRowPos - 1) End If 'Get the instance of the row rowInstance = Trim(Mid(html, firstRowPos, lastRowPos - firstRowPos)) 'Replace if keyword found in row If InStr(pos, rowInstance, keyword, CompareMethod.Text) <> 0 Then newHtml &= rowInstance.Replace(findText, rplText) Else newHtml &= rowInstance End If pos = lastRowPos + 1 Loop sr.Close() 'Save file Dim sw As StreamWriter = New StreamWriter("C:\test.html") sw.AutoFlush = True sw.Write(newHtml) sw.Close() End If End If Quote
IngisKahn Posted September 22, 2005 Posted September 22, 2005 <tr>(?=(.|\s)*(PÅMINN|PÅMINN)(.|\s)*</tr>) That will match the <tr> so you can use .Replace Quote "Who is John Galt?"
kejpa Posted September 22, 2005 Author Posted September 22, 2005 Thanx IngisKahn, it looks like it's going to fit.... Not so used to RegEx in VS (just used it with Perl & PHP) but I will make a go for it and promise to be back if I don't make it ;) Regards /Kejpa Quote
IngisKahn Posted September 22, 2005 Posted September 22, 2005 Heh, sure. Remember to use the MultiLine option. Quote "Who is John Galt?"
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.