您的位置:首页 > Web前端 > HTML

C# 实现读取本地某文件夹中的html文件,然后通过正则表达式获取input控件中的id值

2015-01-05 15:19 579 查看
以下已测试:

html:

<html>
    <head>
        <meta http-equiv=content-type content="text/html;charset=GBK">
    </head>
    <body>
        <input id="__txt"  type="password" />
        <input id="__ttm"  type="button" value="提交" onclick="sub()">
    </body>
    <script>
            function sub(){
                var content = document.getElementById("txt");
                alert(content.value);
            }
    </script>
</html>


C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace tt
{
    class Program
    {
        static void Main(string[] args)
        {
            System.IO.StreamReader sr = new System.IO.StreamReader("f:\\qq.html");
            string ss = "";//存放内容
            ss = sr.ReadToEnd();//内容追加到ss中

            string pat = @"<input[^>]+?id=""([\s\S]+?)""[^>]+>";//获取ID
            Regex r = new Regex(pat, RegexOptions.IgnoreCase);
            Match m = r.Match(ss);
           
            while (m.Success)
            {
                Group g = m.Groups[1];
                string pat1 = @"^_[a-zA-Z_\u0391-\uFFE5]+$";//获取以__开头的字符串
                Match TitleMatch = Regex.Match(g.ToString(),pat1, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                Console.WriteLine(TitleMatch); Console.ReadKey();
                m = m.NextMatch();
            }
        }
    }
}


PS:

1.这里获取的是input控件中id以__(2下划线)开头的值。

2.如果您的正则表达式学得好的话,其实可以将上面2个整合成一个,很抱歉我的正则没怎么研究。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐