小弟毕业有2年了,平时工作的时候也会有一些好的东西,以前总是懒的分享,慢慢的就连自己也忘了,从今天开始记录平时工作中的一些东西给大家分享一下吧,欢迎大家指正。
欢迎大家加群一起讨论技术 QQ群:115180614(群主是江湖)。闲人勿扰。。。
string ma = "<img[^>]+src\s*=\s*['"]([^'"]+)['"][^>]*>";
string image = "<img src="1.png" />dfsdfsdfd计算机阿大姐佛山<img alt="" src="1.png" />";
MatchCollection match = Regex.Matches(image, ma);
for (int i = 0; i < match.Count; i++)
{
if (match[i].Groups[0].Success)
{
//匹配整个字符串,相当于把整个image标签的内容读出来
Label1.Text += match[i].Groups[0].Value.ToString();
}
if (match[i].Groups[1].Success)
{
//匹配小括号里面的东西,也就是图片的地址了
Label2.Text += match[i].Groups[1].Value.ToString();
}
}
string image = "<img src="1.png" />dfsdfsdfd计算机阿大姐佛山<img alt="" src="1.png" />";
MatchCollection match = Regex.Matches(image, ma);
for (int i = 0; i < match.Count; i++)
{
if (match[i].Groups[0].Success)
{
//匹配整个字符串,相当于把整个image标签的内容读出来
Label1.Text += match[i].Groups[0].Value.ToString();
}
if (match[i].Groups[1].Success)
{
//匹配小括号里面的东西,也就是图片的地址了
Label2.Text += match[i].Groups[1].Value.ToString();
}
}
//附图片在下面
这个是最终显示结果。
为初学正则表达的同学附上一张图,可以理解上面的正则的意思。