本次示例效果如下:

 

TextBox1中輸入如下字串:

 

12<table><tbody><tr><td>1<br/>11</td><td>a</td></tr><tr><td>2</td><td>b</td></tr></tbody></table>

 

34<table><tbody><tr><td>3</td><td>c</td></tr><tr><td>4</td><td>d</td></tr></tbody></table>

 

點擊按鈕, 轉換第一個tr下面的td為tr 將TextBox1中的table中第一個tr下面的td 轉換成th ,將結果顯示在TextBox2中

 

12<table><tbody><tr><th>1<br/>11</th><th>a</th></tr><tr><td>2</td><td>b</td></tr></tbody></table>

 

34<table><tbody><tr><th>3</th><th>c</th></tr><tr><td>4</td><td>d</td></tr></tbody></table>

 

轉換第一個tr下面的tr為td 將TextBox2中的table中第一個tr下面的th 轉換成td ,將結果顯示在TextBox3中

 

12<table><tbody><tr><th>1<br/>11</th><th>a</th></tr><tr><td>2</td><td>b</td></tr></tbody></table>

 

34<table><tbody><tr><th>3</th><th>c</th></tr><tr><td>4</td><td>d</td></tr></tbody></table>

 

相關代碼如下:

 

01.Imports System.Text.RegularExpressions
02.
03.
04.Public Class Form1
05.
06.
07. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
08.
09. TextBox2.Text = ConvertFirstTrTdToTh(TextBox1.Text.Trim)
10.
11. TextBox3.Text = ConvertFirstTrThToTd(TextBox2.Text.Trim)
12.
13. End Sub
14.
15.
16. Public Function ConvertFirstTrTdToTh(ByVal strContent As String) As String
17.
18. Dim strOld As String = strContent.Trim
19.
20. 'RegexOptions.Singleline 模式下 .代替任何字元 包括回車換行
21.
22. Dim RegExTable As New Regex("<tbody[^>]*>(.+?)</tr>", RegexOptions.Singleline)
23.
24. Dim RegExFirstTr As New Regex("<tr[^>]*>(.+?)</tr>", RegexOptions.Singleline)
25.
26. Dim RegExTd As New Regex("<td[^>]*>(?<tdContent>.+?)</td>", RegexOptions.Singleline)
27.
28. For Each mTable As Match In RegExTable.Matches(strOld)
29.
30. Dim strFirstTr As String = RegExFirstTr.Match(mTable.Value).Value
31.
32. 'For Each mTd As Match In RegExTd.Matches(strFirstTr)
33.
34. Dim strTdTh As String = RegExTd.Replace(strFirstTr, "<th>${tdContent}</th>")
35.
36. 'Next
37.
38. strOld = New Regex(strFirstTr).Replace(strOld, strTdTh, 1)
39.
40. Next
41.
42. ConvertFirstTrTdToTh = strOld
43.
44.End Function
45.
46.
47. Public Function ConvertFirstTrThToTd(ByVal strContent As String) As String
48.
49. Dim strOld As String = strContent.Trim
50.
51. Dim RegExTable As New Regex("<tbody[^>]*>(.+?)</tr>", RegexOptions.Singleline)
52.
53. Dim RegExFirstTr As New Regex("<tr[^>]*>(.+?)</tr>", RegexOptions.Singleline)
54.
55. Dim RegExTd As New Regex("<th[^>]*>(?<tdContent>.+?)</th>", RegexOptions.Singleline)
56.
57. For Each mTable As Match In RegExTable.Matches(strOld)
58.
59. 'Dim mgFirstTr As MatchCollection = RegExFirstTr.Matches(mTable.Value)
60.
61. Dim strFirstTr As String = RegExFirstTr.Match(mTable.Value).Value
62.
63. 'For Each mTd As Match In RegExTd.Matches(strFirstTr)
64.
65.Dim strTdTh As String = RegExTd.Replace(strFirstTr, "<td>${tdContent}</td>")
66.
67. 'Next
68.
69. strOld = New Regex(strFirstTr).Replace(strOld, strTdTh, 1)
70.
71. Next
72.
73. ConvertFirstTrThToTd = strOld
74.
75. End Function
76.
77.End Class



創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()