using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//类型1的正则表达式
Regex re = new Regex("<tr[^>]*>[\\s]*<td>(?<id>\\d{1,})</td>[\\s]*<td>[\\s]*<a[^>]*>[\\s]*<B><font[^>]*>(?<name>\\w{1,})</font></b>[\\s]*</a>[\\s]*</td>[\\s]*<td>[\\s]*<font[^>]*>\\w{1,}</font>[\\s]*</td>[\\s]*<td>(?<pl>[^>]*[^<]*)</td>[\\s]*");
//类型2的正则表达式
Regex re1 = new Regex("<tr[^>]*>[\\s]*<td>(?<id>\\w*)</td>[\\s]*<td>[\\s]*<B><script>([\\s]*)writeSelection3\\((?<name>\\w*),'dealerCode','\\w*'\\);[\\s]*</script>[\\s]*</b>[\\s]*</td>[\\s]*<td>[\\s]*<font[^>]*>\\w</font>[\\s]*</td>[\\s]*<td>[\\s]*(?<pl>[\\w\\.]*)[\\s]*[\\(\\)\\-]*[\\s]*</td>");
//MatchCollection m=re.Matches(GetTable());
MatchCollection m = re1.Matches(GetTable());
for(int i=0;i<m.Count;i++)
{
Response.Write("序号:" + m[i].Groups["id"].Value + "名称:" + m[i].Groups["name"].Value + "赔率" + m[i].Groups["pl"].Value + "<br/>");
}
}
string GetTable()
{
string path = Server.MapPath("TextFile2.txt");
return File.OpenText(path).ReadToEnd();
}
}
其中\s空白匹配非常有用
<tr bgcolor="#EEEEEE">
<td>1</td>
<td>
<B><script>
writeSelection3(1,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>2</td>
<td>
<B><script>
writeSelection3(2,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>3</td>
<td>
<B><script>
writeSelection3(3,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>4</td>
<td>
<B><script>
writeSelection3(4,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>5</td>
<td>
<B><script>
writeSelection3(5,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>6</td>
<td>
<B><script>
writeSelection3(6,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>7</td>
<td>
<B><script>
writeSelection3(7,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>8</td>
<td>
<B><script>
writeSelection3(8,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>9</td>
<td>
<B><script>
writeSelection3(9,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
36.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>10</td>
<td>
<B><script>
writeSelection3(10,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>11</td>
<td>
<B><script>
writeSelection3(11,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>12</td>
<td>
<B><script>
writeSelection3(12,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
36.50
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>13</td>
<td>
<B><script>
writeSelection3(13,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>14</td>
<td>
<B><script>
writeSelection3(14,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>15</td>
<td>
<B><script>
writeSelection3(15,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>16</td>
<td>
<B><script>
writeSelection3(16,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
30.50
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>17</td>
<td>
<B><script>
writeSelection3(17,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>18</td>
<td>
<B><script>
writeSelection3(18,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
36.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>19</td>
<td>
<B><script>
writeSelection3(19,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>20</td>
<td>
<B><script>
writeSelection3(20,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>21</td>
<td>
<B><script>
writeSelection3(21,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
36.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>22</td>
<td>
<B><script>
writeSelection3(22,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>23</td>
<td>
<B><script>
writeSelection3(23,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
36.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>24</td>
<td>
<B><script>
writeSelection3(24,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>25</td>
<td>
<B><script>
writeSelection3(25,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>26</td>
<td>
<B><script>
writeSelection3(26,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>27</td>
<td>
<B><script>
writeSelection3(27,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>28</td>
<td>
<B><script>
writeSelection3(28,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>29</td>
<td>
<B><script>
writeSelection3(29,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>30</td>
<td>
<B><script>
writeSelection3(30,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>31</td>
<td>
<B><script>
writeSelection3(31,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>32</td>
<td>
<B><script>
writeSelection3(32,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>33</td>
<td>
<B><script>
writeSelection3(33,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
36.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>34</td>
<td>
<B><script>
writeSelection3(34,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>35</td>
<td>
<B><script>
writeSelection3(35,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
36.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>36</td>
<td>
<B><script>
writeSelection3(36,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>37</td>
<td>
<B><script>
writeSelection3(37,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>38</td>
<td>
<B><script>
writeSelection3(38,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>39</td>
<td>
<B><script>
writeSelection3(39,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>40</td>
<td>
<B><script>
writeSelection3(40,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>41</td>
<td>
<B><script>
writeSelection3(41,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>42</td>
<td>
<B><script>
writeSelection3(42,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>43</td>
<td>
<B><script>
writeSelection3(43,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>44</td>
<td>
<B><script>
writeSelection3(44,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>45</td>
<td>
<B><script>
writeSelection3(45,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
35.50
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>46</td>
<td>
<B><script>
writeSelection3(46,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>47</td>
<td>
<B><script>
writeSelection3(47,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>48</td>
<td>
<B><script>
writeSelection3(48,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>
<tr bgcolor="#EEEEEE">
<td>49</td>
<td>
<B><script>
writeSelection3(49,'dealerCode','hd2114');
</script></b>
</td>
<td>
<font color="#CC0000">0</font>
</td>
<td>
37.00
(-)
</td>
<td>0</td>
<td>0</td>
<td>
-
</td>
<td></td>
</tr>