<a href="download.aspx">1</a><a href="http://www.baidu.com/download.aspx">2</a><a href="/download.aspx">如上代码,我想仅仅只匹配download.aspx出来,凡是带http开头或者说带有"/"的均不匹配,请问该如何写此表达式呢?还望高手多多赐招。很困扰啊。

解决方案 »

  1.   

    string s = @"<a href=""download.aspx"">1</a><a href=""http://www.baidu.com/download.aspx"">2</a><a href=""/download.aspx"">";
    MatchCollection matches = Regex.Matches(s, @"(?is)<a\s+href=""[^./]+\.[^./]+"">.*?</a>");
    foreach (Match match in matches)
    Response.Write(match.Value + "<br/>");
      

  2.   

    你好。不要带a href行不,因为有些地址是flash的,还有些是图片,以上只是举例,实际目的是要取出地址
      

  3.   

    string s = @"<a href=""download.aspx"">1</a><a href=""http://www.baidu.com/download.aspx"">2</a><a href=""/download.aspx""><img src='a.jpg' />";
    MatchCollection matches = Regex.Matches(s, @"(?is)(?:href|src)=(['""])(?<地址>[^./]+\.[^./]+)\1");
    foreach (Match match in matches)
    Response.Write(match.Groups["地址"].Value + "<br/>");
      

  4.   

    你好。感谢你热情的帮助。我可能表达有问题。我的意思是只想取得download.aspx  不需要取得完整的<a href="download.aspx">1</a> 我的目的是想将本地的非根目录的全部资源最终取出来,全部转为 /download.aspx的根目录形式,而已有/开头的和http外链的则不用处理。请问咋办
      

  5.   

    4楼给你的不就是地址吗?没包括标签本身啊。只要是href和src里的都能取到。
      

  6.   

    temp = Regex.Replace(temp, @"(([\w-]+\/)+[\w-]+\.js|[\w-]+\.js)", "/$1");比如这是我实际的写法。可以取得如sdgad/sadgasdg/asdgasdg.js的,但如果这个是统计代码,以http开头的,也给加/了,就没用了。
      

  7.   

    四楼的有些问题,如果是<a href=""lll/download.aspx"">1</a>就取不到了。这个我想是要取掉,最终加/变成根目录的。但取不了
      

  8.   

    4楼的代码运行后输出:
    download.aspx
    a.jpgGroups["地址"]是名为“地址”的正则捕获组。
      

  9.   

    四楼的有些问题。我只是想排除以http开头和以/开头的。其它中间可带/
      

  10.   

    这个试下:
    string s = @"<a href=""download.aspx"">1</a><a href=""http://www.baidu.com/download.aspx"">2</a><a href=""/download.aspx""><img src='a/a.jpg' /><a href='/p.html'></a>";
    MatchCollection matches = Regex.Matches(s, @"(?is)(?:href|src)=(['""])(?!href|/)(?<地址>[^.]+\.[^./]+)\1");
    foreach (Match match in matches)
    Response.Write(match.Groups["地址"].Value + "<br/>");
      

  11.   

    输出:
    download.aspx
    a/a.jpg
      

  12.   

    发现问题了。
    比如原来是<a href="download.aspx" target="_blank" style="color:#FFF; text-decoration:none;">的东西,结果却错误的取出 download.aspx" target="_blank" style="color:#FFF; text-decoration:none;  能再完美下吗?也就是取到最后一个引号内的东西了,中途有引号的计算在内了
      

  13.   

    正则改成:@"(?is)(?:href|src)=(['""])(?!href|/)(?<地址>[^."']+\.[^./"']+)\1"
    试试。
      

  14.   

    应该是:@"(?is)(?:href|src)=(['""])(?!href|/)(?<地址>[^.""']+\.[^./""']+)\1"
      

  15.   

    还有一种情况,style="background-image:url(images/bg.gif);">
    样式中的路径如何也能取掉呢。
      

  16.   

    在吗?兄弟,我一直在等你。<script src = "http://s4.cnzz.com/stat.php?id=123456&web_id=123456&show=pic" language="JavaScript"></script>
    这种情况会错误的匹配,请问如果改动能排除忽略这个