string ptr = @"[^/](?:img)\s*(?:\s*src\s*=\s*(?<s1>['"\>]?)(?:(?:http://(?<host>[^/]*))?(?<path>[^/].*?)(?<file>.*?))\k<s1>|\s*\S*\s*=\s*(?<sx>['"\>]?)(?<other>\S*)\k<sx>)*>";
string str = @"<img alt=test alt=\"test\" src='template/图images/我footer_safe1.jpg' border=\"3\">
dsfdsf
<img alt=\"test\" border='3' src=\"http://www.baidu.com/template/图images/你footer_safe2.jpg\"><img border=\"3\" src=图images/他footer_safe3.jpg alt=\"test\"><img border=\"3\" src=未footer_safe4.jpg alt=\"test\">
";
要求:
1、不管url在任何位置,都提取了出来,形式如:
//-----------------------------------------------------------------
//host path file
// /tempate/图images/ 我footer_safe1.jpg
//www.baidu.com /tempate/图images/ 你footer_safe2.jpg
// /tempate/图images/ 他footer_safe3.jpg
// 未footer_safe4.jpg
//-----------------------------------------------------------------
2、不管url位于任何位置,均可一次性提取。
3、可以支持字符串中包含中文字符。这样提取的目的是,可以把这个url换成其它地址,再替换回去。
但是,我觉得这个正则表达式写得不伦不类的,
更主要的是:这个提取URL没有完全实现,请指教。
string str = @"<img alt=test alt=\"test\" src='template/图images/我footer_safe1.jpg' border=\"3\">
dsfdsf
<img alt=\"test\" border='3' src=\"http://www.baidu.com/template/图images/你footer_safe2.jpg\"><img border=\"3\" src=图images/他footer_safe3.jpg alt=\"test\"><img border=\"3\" src=未footer_safe4.jpg alt=\"test\">
";
要求:
1、不管url在任何位置,都提取了出来,形式如:
//-----------------------------------------------------------------
//host path file
// /tempate/图images/ 我footer_safe1.jpg
//www.baidu.com /tempate/图images/ 你footer_safe2.jpg
// /tempate/图images/ 他footer_safe3.jpg
// 未footer_safe4.jpg
//-----------------------------------------------------------------
2、不管url位于任何位置,均可一次性提取。
3、可以支持字符串中包含中文字符。这样提取的目的是,可以把这个url换成其它地址,再替换回去。
但是,我觉得这个正则表达式写得不伦不类的,
更主要的是:这个提取URL没有完全实现,请指教。
解决方案 »
- c# 删除treeview(web)节点删不掉
- 如何在winform 中修改app.config的值?
- 关于winform窗体重绘图形闪烁的问题
- 使用serialport进行串口通讯时 接收的数据 如何在控件上显示?
- 如何在 linux 平台 运行 net程序
- 问一个enterprise library 2006一月版daab中的关于执行存储过程的问题?
- 怎样在DataGrid中实现数量,金额合计???
- C# 中如何得到当前程序的路径?GetPath()如何使用?在线等待!
- C#中怎么实现代码打印rdlc报表(webform)
- 谁来帮我看看取radiobutton的值呀
- WinForm打包问题求解
- C#中用指针操作字符串的问题
将就了,就这样。
string str = @"<img alt=test alt=""test"" src='template/图images/我footer_safe1.jpg' border=""3"">
dsfdsf
<img alt=""test"" border='3' src=""http://www.baidu.com/template/图images/你footer_safe2.jpg""><img border=""3"" src=图images/他footer_safe3.jpg alt=""test""><img border=""3"" src=未footer_safe4.jpg alt=""test"">";
foreach (Match m in Regex.Matches(str, @"<img[^>]*src=(['""]*)(http://(?<host>[^\.]+\.[^\.]+\.[^/]+)/)?(?<path>([^/]+/)*)(?<file>[^\.]+\.[^\s]+)\1[^>]*>"))
Console.WriteLine(m.Groups["host"].Value + "\t" + m.Groups["path"].Value + "\t" + m.Groups["file"].Value);
void Main()
{
string str = @"<img alt=test alt=""test"" src='template/图images/我footer_safe1.jpg' border=""3"">
dsfdsf
<img alt=""test"" border='3' src=""http://www.baidu.com/template/图images/你footer_safe2.jpg""><img border=""3"" src=图images/他footer_safe3.jpg alt=""test""><img border=""3"" src=未footer_safe4.jpg alt=""test"">";
Console.WriteLine("host\t\t\tpath\t\t\tfile");
Regex reg=new Regex(@"(?i)(https?://)?www\.[^/]+");
foreach(Match m in Regex.Matches(str,@"(?i)<img[^>]*?src=(['""\s]?)(?<url>[^'""\s]+)\1[^>]*?>"))
{
string url=m.Groups["url"].Value;
string host=reg.IsMatch(url)?reg.Match(url).Value:"";
string file=System.IO.Path.GetFileName(url);
if(string.IsNullOrEmpty(host))
{
Console.WriteLine("{0}\t\t\t{1}\t\t{2}",host,url.Replace(file,""),file);
}
else
{
Console.WriteLine("{0}\t{1}\t{2}",host,url.Replace(host,"").Replace(file,""),file);
}}}
/*
host path file
template/图images/ 我footer_safe1.jpg
http://www.baidu.com /template/图images/ 你footer_safe2.jpg
图images/ 他footer_safe3.jpg
未footer_safe4.jpg*/
<img[^>]*src=(['""]*)(http://(?<host>[^\.]+\.[^\.]+\.[^/]+))?(?<path>(/?[^/>].)*/)?(?<file>.*?)\.?(?<ext>jpg|gif|png|bmp)\1[^/>]*/?>
<img[^>]*src=(['""]*)(http://(?<host>[^\.]+\.[^\.]+\.[^/]+))?(?<path>(/?[^/>].)*/)?(?<file>.*?)\.?(?<ext>jpg|gif|png|bmp)\1[^/>]*/?>