日期:2014-05-17  浏览次数:20886 次

高手帮忙写个正则
<img src='/UploadFile/Images/1-1_20121113165342.jpg' width='990' height='142' />文章内容文章内容文章内容
                <img src='/UploadFile/Images/1-1_20121113165342.jpg' width='990' height='142' />文章内容文章内容文章内容
                <img src='/UploadFile/Images/1-1_20121113165342.gif' width='990' height='142' />文章内容文章内容文章内容
                <a href='/UploadFile/Excel/1-1_20121113165342.xls' width='990' height='142' />链接链接链接链接链接</a>
                <a href='/UploadFile/Word/1-1_20121113165342.doc' width='990' height='142' />链接链接链接链接</a>

匹配
/UploadFile/Word/1-1_20121113165342.doc
/UploadFile/Excel/1-1_20121113165342.xls
/UploadFile/Images/1-1_20121113165342.gif

------解决方案--------------------

            System.IO.StreamReader reader = new System.IO.StreamReader("e:\\1.txt",System.Text.Encoding.Default);
            string str = reader.ReadToEnd();
            Regex reg = new Regex(@"(?is)(?<=<img src=')[^']+(?=')");
            MatchCollection mc = reg.Matches(str);
            foreach (Match m in mc)
            {
                MessageBox.Show(m.Value);
            }

把<img src 换成 <a href 即可选出另外两组
------解决方案--------------------
string str = System.IO.File.ReadAllText("D:\\1.txt", Encoding.Default);
            string pattern = @"(?<=(href
------解决方案--------------------
src)=[""'])[^>'""]+\.(xls
------解决方案--------------------
doc
------解决方案--------------------
gif)(?=[""'])";
            var ary = Regex.Matches(str, pattern).OfType<Match>().Select(t => t.Value).ToList();
           
------解决方案--------------------
(?i)(?<=(?:src
------解决方案--------------------
href)=(['"]?))[^'"]+(?=\1)