截取的html中有大量的图片信息,比如<P>
<DIV id=hystocklist align=center>
<IMG src="http://www.csdn.net/images/minpic/pic570/IF1006.png" border=0></DIV>
<P>aaaaaaaa</P>
<P>bbbbbbbb</P>
<IMG src="http://www.csdn.net/images/minpic/pic570/IF1234.png" border=0>
<P>ccccccc</P>
<P><SPAN id=spTopic>ddddddddd</SPAN></P>
</P>打算对图片标记用正则做替换成如下结果<P>
<DIV id=hystocklist align=center>
<IMG src="http://192.168.1.1/xxx/images/20100525/IF1006.png" border=0></DIV>
<P>aaaaaaaa</P>
<P>bbbbbbbb</P>
<IMG src="http://192.168.1.1/xxx/images/20100525/IF1234.png" border=0>
<P>ccccccc</P>
<P><SPAN id=spTopic>ddddddddd</SPAN></P>
</P>应该如何处理其中
http://192.168.1.1/xxx/images/20100525/IF1006.png192.168.1.1 为服务器地址 变量
xxx 为项目名称 变量
20100525为当天日期谢谢!
<DIV id=hystocklist align=center>
<IMG src="http://www.csdn.net/images/minpic/pic570/IF1006.png" border=0></DIV>
<P>aaaaaaaa</P>
<P>bbbbbbbb</P>
<IMG src="http://www.csdn.net/images/minpic/pic570/IF1234.png" border=0>
<P>ccccccc</P>
<P><SPAN id=spTopic>ddddddddd</SPAN></P>
</P>打算对图片标记用正则做替换成如下结果<P>
<DIV id=hystocklist align=center>
<IMG src="http://192.168.1.1/xxx/images/20100525/IF1006.png" border=0></DIV>
<P>aaaaaaaa</P>
<P>bbbbbbbb</P>
<IMG src="http://192.168.1.1/xxx/images/20100525/IF1234.png" border=0>
<P>ccccccc</P>
<P><SPAN id=spTopic>ddddddddd</SPAN></P>
</P>应该如何处理其中
http://192.168.1.1/xxx/images/20100525/IF1006.png192.168.1.1 为服务器地址 变量
xxx 为项目名称 变量
20100525为当天日期谢谢!
foreach (Match m in re.Matches(str))
{
Console.WriteLine(m.Value);
}
MatchCollection matchs = Regex.Matches(fckStr, @ " <img\s[^> ]*src=([ " " ']*)(? <src> [^ ' " "]*)\1[^> ]*> ", RegexOptions.IgnoreCase);
foreach (Match m in matchs) {
Response.Write(m.Groups[ "src "].Value+ " <br> ");
}
上面这种情况是否需要处理?
private void button3_Click(object sender, EventArgs e)
{
string str=richTextBox1.Text; str=Regex.Replace(str,@"(?is)(<img[^>]*src=(?<yh>[""']?))(?<q>.*?)(?<=/?)(?=\w+\.\w+\b['"">\s])(?<f>[^""'>\s]+)(?=\k<yh>)",@"${1}http://192.168.1.1/xxx/images/20100525/${f}");
richTextBox1.AppendText("\n\n\n\n\n" + str); }
richTextBox1中的内容:<IMG src=http://www.csdn.net/images/minpic/pic570/IF1006.png>
<IMG width=100 src=http://www.csdn.net/images/minpic/pic570/IF1006.png border=0>
<IMG src='http://www.csdn.net/images/minpic/pic570/IF1006.png' border=0><IMG src="http://www.csdn.net/images/minpic/pic570/IF1006.png" border=0>
<IMG src='http://www.csdn.net/images/minpic/pic570/IF1006.png'>
<IMG src="http://www.csdn.net/images/minpic/pic570/IF1006.png">
<IMG src="IF1234.png" border=0>
<IMG src=IF1234.png>
<IMG src=http://192.168.1.1/xxx/images/20100525/IF1006.png>
<IMG width=100 src=http://192.168.1.1/xxx/images/20100525/IF1006.png border=0>
<IMG src='http://192.168.1.1/xxx/images/20100525/IF1006.png' border=0><IMG src="http://192.168.1.1/xxx/images/20100525/IF1006.png" border=0>
<IMG src='http://192.168.1.1/xxx/images/20100525/IF1006.png'>
<IMG src="http://192.168.1.1/xxx/images/20100525/IF1006.png">
<IMG src="http://192.168.1.1/xxx/images/20100525/IF1234.png" border=0>
<IMG src=http://192.168.1.1/xxx/images/20100525/IF1234.png>
{
string html = @"<P>
<DIV id=hystocklist align=center>
<IMG src=""http://www.csdn.net/images/minpic/pic570/IF1006.png"" border=0></DIV>
<P>aaaaaaaa</P>
<P>bbbbbbbb</P>
<IMG src=""http://www.csdn.net/images/minpic/pic570/IF1234.png"" border=0>
<P>ccccccc</P>
<P><SPAN id=spTopic>ddddddddd</SPAN></P>
</P>";
string server = "192.168.1.1";//服务器名
string project = "xxx";//项目名
string date = "20100525";//日期
string html_parsed = Regex.Replace(html, @"(?is)(<img\s*src=""(?:http://)?).*?/(?<filename>[^""/]+)""", delegate(Match m) { return m.Groups[1].Value + server + "/" + project + "/images/" + date + "/" + m.Groups["filename"].Value; });
Console.WriteLine(html);
Console.WriteLine("----------------------------------");
Console.WriteLine(html_parsed);
}
修改对应变量就可以了。
string projectName = textBox2.Text;
string today = DateTime.Now.ToString("yyyyMMdd");
Regex reg = new Regex(@"(?i)(<img[^>]*?\ssrc=(['""]?))(?:[^'""\s>/]*/)*([^'""\s>.]+\.[^'""\s>]+\2[^>]*>)");
string result = reg.Replace(yourStr, "$1http://" + ip + "/" + projectName + "/images/" + today + "/$3");
richTextBox2.Text = result;