用C#和正则表达式截取html代码

【IT168技术文档】如何使用C#和正则表达式截取html代码呢,如何处理html代码中的\r\n这样的字符呢,下面我们来一起研究一下,先从截取目标开始。

一、代码说明

 1.以下html表示收到的3个组的信息,如果含有"unread.gif"表示未读消息,否则表示已读信息。

 2.截取未读消息和已读消息的条数和theUrl

 3.要将未读信息和已读信息分开放入两个组里。

 <div class="dxx_of" />

 <div class="dxx1" ><img src="http://www.microsoft.com/i2/unread.gif" width="14" height="10" title="" /></div>

 <div class="dxx2">

 <table class="aa" cellpadding="0" cellspacing="0" >

 <colgroup>

 <col width="463" />

 </colgroup>

 <tbody>

 <tr base>

 <td valign="top" onclick="javascript:document.location='thUrl';" >

 wa

 <div><span class='c9'>共6条会话</span><a href="thUrl" class="sl">+展开</a></span></div>

 <span class="c9"></span>

 </td>

 </tr>

 </tbody>

 </table>

 </div>

 <div class="c"></div>

 </div>

 <div class="dxx_of" />

 <div class="dxx1" ></div>

 <div class="dxx2">

 <table class="aa" cellpadding="0" cellspacing="0" >

 <colgroup>

 <col width="463" />

 </colgroup>

 <tbody>

 <tr base>

 <td valign="top" onclick="javascript:document.location='thUrl';" >

 wa

 <div><span class='c9'>共3条会话</span><a href="thUrl1" class="sl">+展开</a></span></div>

 <span class="c9"></span>

 </td>

 </tr>

 </tbody>

 </table>

 </div>

 <div class="c"></div>

 </div>

 <div class="dxx_of" />

 <div class="dxx1" ></div>

 <div class="dxx2">

 同上很多html内容

 </div>

 <div class="c"></div>

 </div>

二、官方样例

 用C#和正则表达式如何对上例的代码进行截取呢,我们来看看微软官方发布的样例。

 示例

 System.Text.RegularExpressions.RegexOptions 。

 C#

 class TestRegularExpressions

 {

 static void Main()

 {

 string[] sentences =

 {

 "cow over the moon",

 "Betsy the Cow",

 "cowering in the corner",

 "no match here"

 };

 string sPattern = "cow";

 foreach (string s in sentences)

 {

 System.Console.Write("{0,24}", s);

 if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))

 {

 System.Console.WriteLine(" (match for '{0}' found)", sPattern);

 }

 else

 {

 System.Console.WriteLine();

 }

 }

 // Keep the console window open in debug mode.

 System.Console.WriteLine("Press any key to exit.");

 System.Console.ReadKey();

 }

 }

 /* Output:

 cow over the moon (match for 'cow' found)

 Betsy the Cow (match for 'cow' found)

 cowering in the corner (match for 'cow' found)

 no match here

 */

 以上代码是一个控制台应用程序,用于对数组中的字符串执行简单的不区分大小写的搜索。给定要搜索的字符串和包含搜索模式的字符串后,静态方法 Regex.IsMatch 将执行搜索。使用第三个参数指示忽略大小写。

 实际可以使用以下代码来匹配。

Regex regex = new Regex("<div class=\"dxx_of\" .+/>.+(?<htmlCode>.+).+<div class=\"c\"></div>");

 MatchCollection matchs = regex.Matches(resultHtml);

 if(maths.Count>0)

 strig html = matchs[0].Groups["htmlCode"].Value;

  但是正则的"."只能匹配不含\n的任何字符,可是HTML代码中有很多\r\n。

三、如何处理特殊字符

 如果不重要可以用string Replace方法将 \r\n替换掉,下面这个方法可以有效的将其分为3个组。

 C#

 class TestRegularExpressionValidation

 {

 static void Main()

 {

 string[] numbers =

 {

 "123-456-7890",

 "444-234-22450",

 "690-203-6578",

 "146-893-232",

 "146-839-2322",

 "4007-295-1111",

 "407-295-1111",

 "407-2-5555",

 };

 string sPattern = "^\d{3}-\d{3}-\d{4}$";

 foreach (string s in numbers)

 {

 System.Console.Write("{0,14}", s);

 if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern))

 {

 System.Console.WriteLine(" - valid");

 }

 else

 {

 System.Console.WriteLine(" - invalid");

 }

 }

 // Keep the console window open in debug mode.

 System.Console.WriteLine("Press any key to exit.");

 System.Console.ReadKey();

 }

 }

 /* Output:

 123-456-7890 - valid

 444-234-22450 - invalid

 690-203-6578 - valid

 146-893-232 - invalid

 146-839-2322 - valid

 4007-295-1111 - invalid

 407-295-1111 - valid

 407-2-5555 - invalid

 */

 以上代码是一个控制台应用程序,此程序使用正则表达式验证数组中每个字符串的格式。验证要求每个字符串具有电话号码的形式,即用短划线将数字分成三组,前两组各包含三个数字,第三组包含四个数字。这是使用正则表达式 ^\d{3}-\d{3}-\d{4}$ 完成的。