1.一个源字符串:
<a href=aa.asp>aaaa</a>
 <a href="http://www.news365.com.cn/yw/t20050822_621630.htm" target=_blank>
<nobr><font size=2>文新传媒1</font></nobr></a>
 <a href="http://www.news365.com.cn/yw/t20050822_621630.htm" target=_blank>
<nobr><font size=2>文新传媒2</font></nobr></a>
2.源字符的内容可能是变化的,但我们已知道此字符串有标题为"文新传媒2"的超链接。
3.我想得到字符串:
<a href="http://www.news365.com.cn/yw/t20050822_621630.htm" target=_blank>
<nobr><font size=2>文新传媒2</font></nobr></a>4.
我用:[^>]*(?<title><a.*?文新传媒2.*?a>)  试了,但没有取值成功。请各位高手帮助解决下此问题。谢啦!

解决方案 »

  1.   

    string p = @"<a\s*.*文新传媒2.*</a>";
      

  2.   

    string p = @"<a\s*.*文新传媒2.*?</a>";Regex reg = new Regex(p,RegexOptions.IgnoreCase);
    Match m = reg.Match("sdfas><a href=\"http://www.news365.com.cn/yw/t20050822_621630.htm\" target=_blank><nobr><font size=2>文新传媒2</font></nobr></a><dsfsadfla>adf</a>");Console.WriteLine(m.Value);
      

  3.   

    老兄,又见到你了,真好!
    你在源字符串:<a href=aa.asp>aaaa</a>
     <a href="http://www.news365.com.cn/yw/t20050822_621630.htm" target=_blank>
    <nobr><font size=2>文新传媒1</font></nobr></a>
     <a href="http://www.news365.com.cn/yw/t20050822_621630.htm" target=_blank>
    <nobr><font size=2>文新传媒2</font></nobr></a>
    中用你的:string p = @"<a\s*.*文新传媒2.*?</a>";
    我试了,匹配出来的结果不是:
    <a href="http://www.news365.com.cn/yw/t20050822_621630.htm" target=_blank>
    <nobr><font size=2>文新传媒2</font></nobr></a>
      

  4.   

    public static Regex regex = new Regex(
        @"<a[^>]*>[\s\S]{1,30}?文新传媒2[\s\S]*?</a>",
        RegexOptions.IgnoreCase | RegexOptions.Compiled);
      

  5.   

    楼上的老兄:你的表达式可以匹配出来。
    我想部下:
    "<a[^>]*>[\s\S]{1,30}?文新传媒2[\s\S]*?</a>", 中的{1,30} 能讲下是什么意思吗,为这样写呢?
      

  6.   

    楼上老兄:fancyf(凡瑞)
    用你的表达式在匹配:
    类似于这样的字符串时:
    <a href="http://www.news365.com.cn/yw/t20050822_621630.htm" target=_blank>文新传媒3</a>
    就无法匹配出结果来。
      

  7.   

    改成{0,30}<a[^>]*>[\s\S]{0,30}?文新传媒3[\s\S]*?</a>这个表达式不是通用的,完全针对你给的字符串进行匹配,别指望能匹配所有的类似内容
    除非你给出更多的例子,再进行针对的修改
      

  8.   

    fancyf(凡瑞)老兄:
    问一个另外的问题:
    我有个页面文件如aa.htm 有500k, 我想把其中的字符串:<a href=1.htm>文新传媒</a>
    替换为:hello<a href=1.htm>文新传媒</a>
    用什么方法速度快一点。我用的方法是:
    Regex regex2 = new Regex(enTitle,RegexOptions.IgnoreCase | RegexOptions.Compiled);
    sHtml=regex2.Replace(sHtml,"${title}工作组");  
    运行程序后功能可以实现,但我发现在匹配过程中cpu使用率为100%,持续时间为2秒才运算出结果,
    这样的速度太慢了!! 而且cup怎么为100%呢?!!老兄可以好一点的方法?谢谢!
      

  9.   

    正则本来就不快,只是方便而已。
    你的enTitle是什么?怎么读的文件?