如是题说.
获得图片的绝对地址.

解决方案 »

  1.   

    public SiteGetIMG(String strVal)
            {
                if (!String.IsNullOrEmpty(strVal))
                {
                    SetupItem SiteSet = SiteCaches.Setup;
                    Regex objRex = new Regex(@"((http|https|ftp):(\/\/|\\\\){1}(([A-Za-z0-9_-])+[.]){1,}(net|com|cn|org|cc|tv|de|us|[0-9]{1,3})(\S*\/)((\S)+[.]{1}(gif|jpg|jpeg|png)))", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    MatchCollection objMatchs = objRex.Matches(strVal);
                    String strFileDate = "Attach/month_" + DateTime.Now.ToString("yyMM");
                    String strFileFold = SiteGlobal.Locale + strFileDate;
                    String strFilePath = HttpContext.Current.Server.MapPath(strFileFold);
                    if (!Directory.Exists(strFilePath))
                    {
                        Directory.CreateDirectory(strFilePath);
                    }
                    String strFileName = String.Empty;
                    AttachItem objAttach;
                    HttpWebRequest objRequest;
                    HttpWebResponse objResponse;
                    Stream objStream;
                    FileStream objFile;
                    Int32 intCount = 1;
                    Uri strURL;
                    AttachList objAttachList = new AttachList();
                    foreach (Match objMat in objMatchs)
                    {
                        objAttach = new AttachItem();
                        objAttach.Author = SiteCaches.Guest;
                        objAttach.Address = objAttach.Author.Address;
                        strURL = new Uri(objMat.Groups[1].Value);
                        if (strURL.Host != new Uri(SiteSet.Address).Host)
                        {
                            objAttach.Prefix = Function.Random(6);
                            objAttach.Suffix = strURL.AbsoluteUri.Substring(strURL.AbsoluteUri.LastIndexOf('.') + 1);
                            objAttach.Publish = DateTime.Now;
                            objAttach.Filename = objAttach.Publish.ToString("HHmmss") + "_" + intCount.ToString();
                            strFileName = objAttach.Prefix + "_" + objAttach.Filename + "." + objAttach.Suffix;
                            try
                            {
                                objAttach.ID = Guid.NewGuid().ToString();
                                objRequest = (HttpWebRequest)WebRequest.Create(strURL);
                                objResponse = (HttpWebResponse)objRequest.GetResponse();
                                objStream = objResponse.GetResponseStream();
                                objAttach.Filesize = (Int32)objResponse.ContentLength;
                                if (objAttach.Filesize <= SiteSet.LimFile)
                                {
                                    objAttach.Mimetype = objResponse.ContentType;
                                    objFile = new FileStream(Path.Combine(strFilePath, strFileName), FileMode.Create, FileAccess.Write);
                                    this.ReadWriteStream(objStream, objFile);
                                    objAttachList.Add(objAttach);
                                    this._OutResult = strVal.Replace(strURL.AbsoluteUri, SiteSet.Address + "/" + strFileDate + "/" + strFileName);
                                }
                                objStream.Close();
                            }
                            catch { }
                            intCount++;
                        }
                    }
                    BitracServices.InsertAttach(objAttachList);
                }
            }
            private void ReadWriteStream(Stream rStream, Stream wStream)
            {
                Int32 intLength = 32768;
                Byte[] objBuff = new Byte[intLength];
                Int32 intByteRead = rStream.Read(objBuff, 0, intLength);
                while (intByteRead > 0)
                {
                    wStream.Write(objBuff, 0, intByteRead);
                    intByteRead = rStream.Read(objBuff, 0, intLength);
                }
                rStream.Close();
                wStream.Close();
            }
      

  2.   

    (http:\/\/[^/\\]+)(.*)(?=/[^/\\]*)取这个结果:
    $2/ex:http://topic.csdn.net/u/20080202/09/5b4b090c-6727-4bb7-ad35-1b0bc8b46384.html$1_$2_$3Result:
    http://topic.csdn.net_/u/20080202/09_/5b4b090c-6727-4bb7-ad35-1b0bc8b46384.html
      

  3.   

    (?:http|https):\/\/[^/\\]+(.*)(?=/[^/\\]*)$1
      

  4.   

    using System;
    using System.Data;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Net;namespace GrabStockSystem
    {
        class Grab
        {
            static void Main(string[] args)
            {
                //申明这个是最简单的,基本的,如果你要做获取的东西,数据量大的话,那么千万不要用webclient,这个超时不好处理
                WebClient myweb = new WebClient();
                myweb.Credentials = CredentialCache.DefaultCredentials;            byte[] mybyte = myweb.DownloadData("http://csdn.net/");//传入网址
                string mystring = Encoding.Default.GetString(mybyte);//得到源代码
                Regex Re = new Regex(@"<img.*?src=(""|')?http(?<imageurl>[\s\S]*?)(""|'|\s)");//正则
                MatchCollection Mc = Re.Matches(mystring);
                foreach (Match M in Mc)
                {
                    Console.WriteLine("http"+M.Groups["imageurl"].Value);
                }
                Console.ReadKey();
            }
          
        }
    }
      

  5.   

    欢迎大家.来群:7729746 〓 .Net/C#交流区〓..交流,项目合作.源码开发.共同研究学习;
      

  6.   

    我是楼主
    我是楼主
    我是楼主
    我是楼主
    我是楼主那我只想找.jpg的字符呢??????????????????????????????????????????????????
      

  7.   

    如果是找图片,还是通过分析img的好,再有就是图片不一定是绝对路径,你需要根据情况转换一下
      

  8.   

    获取网页图片的地址无非就是取得目标网页的所有html代码,然后用正则结合你自己的规则进行解析,然后就得到了
    这里无非两个难点
    1,获得目标网页的html代码
    2,组合自己的正则(要注意是最短匹配,呵呵,否则你很可能得到一个匹配项)
    正则只能自己去根据规则写了,而获得目标网页的代码网上资料一堆(满足你的C#条件)
      

  9.   

    如果有dom对象,比如你用webbrowser,遍历dom就可以了,如果是文本,比如你用webclient,只能用正则了
      

  10.   

    正则 这样写差不多吧?<img.*src.*=.*> 
      

  11.   

    to:那我只想找.jpg的字符呢??????????????????????????????????????????????????那么字符串肯定是:http:任意字符.jpg那么你可以写成
    http:(.*?).jpg
      

  12.   

                    MatchCollection mXml = Regex.Matches(sb.ToString(), @"(?<=xmlpath=)(?:.|\n)*?(?=xml/>)",
                                    RegexOptions.IgnoreCase);
                    foreach (Match mh in mXml)
                    {
                        CopyFile(strSub(strSourceHtm) + "\\" + mh.Value.ToString() + "xml", strTarget + "\\" + mh.Value.ToString() + "xml");
                    }
    这个是取网页中xml的正则表达式.还有就是.当取img的时候.别忘了还有背景.backgroup