发布:2020/6/9 9:10:23作者:管理员 来源:本站 浏览次数:1086
//读取网页htmlConsole.ReadKey();
图解正则
朋友需要截取img标签的src 和data-url 跟上面差不多。。顺便附上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
string text =File.ReadAllText(Environment.CurrentDirectory + "//test.txt" , Encoding.GetEncoding( "gb2312" ));
string prttern = "<img(\\s*(src=\"(?<src>[^\"]*?)\"|data-url=\"(?<dataurl>[^\"]*?)\"|[-\\w]+=\"[^\"]*?\"))*\\s*/>" ;
var maths = Regex.Matches(text, prttern);
//抓取出来写入的文件
using (FileStream w = new FileStream(Environment.CurrentDirectory + "//wirter.txt" , FileMode.Create))
{
for ( int i = 0; i < maths.Count; i++)
{
byte [] bs = Encoding.UTF8.GetBytes( string .Format( "图片src:{0}, 图片data-url:{1}" , maths[i].Groups[ "src" ].Value,
maths[i].Groups[ "dataurl" ].Value) + "\r\n" );
w.Write(bs, 0, bs.Length);
Console.WriteLine();
}
}
|
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4