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没有完全实现,请指教。 

解决方案 »

  1.   

    <img[^>]*src=(("|')(?<src>.*?)\k<2>|(?<src>\S+))${src} 就是楼主要的 url
      

  2.   

    [^/](?:img)\s*(?:\s*src\s*=\s*(?<s1>['"\>]?)(?:(?:http://(?<host>[^/]*))?(?<path>/?[^/].*/)(?<file>.*?(?:jpg|gif|png)))\k<s1>|\s*\S*\s*=\s*(?<sx>['"\>]?)(?<other>\S*)\k<sx>)*>
    将就了,就这样。
      

  3.   


                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);
      

  4.   


    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*/
      

  5.   

    (?<=src\=(['"\s]*?))[^'"\s]+(?=\1) 这个行不行??
      

  6.   

    要把:域名host、路径path、文件file、扩展名ext全部要分离出来,谢谢.
      

  7.   

    我把ojlovecd的改了一下,这样就可以了.
    <img[^>]*src=(['""]*)(http://(?<host>[^\.]+\.[^\.]+\.[^/]+))?(?<path>(/?[^/>].)*/)?(?<file>.*?)\.?(?<ext>jpg|gif|png|bmp)\1[^/>]*/?>
      

  8.   

    <img[^>]*src=(['""]*)(http://(?<host>[^\.]+\.[^\.]+\.[^/]+)/)?(?<path>([^/]+/)*)(?<file>[^\.]+\.[^\s]+)\1[^>]*>
    <img[^>]*src=(['""]*)(http://(?<host>[^\.]+\.[^\.]+\.[^/]+))?(?<path>(/?[^/>].)*/)?(?<file>.*?)\.?(?<ext>jpg|gif|png|bmp)\1[^/>]*/?>