我需要一个正则表达式匹配以下格式:
http://g.live.com/<LinkFile>/<LinkID>  (基本格式)例如:
http://g.live.com/8SE.en-us.14.0/JewelMenu1
http://g.msn.com/5meen_us/122 
http://g.live.com/1rew3set/en_error?!draug_eciohc 
http://g.live.com/1rew3set/en_ceip 
http://g.live.com/1rew3set/en_toolbar现在我有一正则表达式:(?:(?:(?:http|ftp|gopher|telnet|news)://)(?:w{3}\.)?(?:[a-zA-Z0-9/;\?&=:\-_\$\+!\*'\(\|\\~\[\]#%\.])+)能够匹配这些,但是也可以匹配形如:
http://g.live.com/1rew3set/en_ceip.aspx 
http://g.live.com/1rew3set/en_toolbar.html我不需要它匹配到这样的,简单来说,就是求一正则表达式,匹配URL但是最后跟的不是.aspx或者.html等内容在线等

解决方案 »

  1.   

    要求匹配:
    http://g.live.com/8SE.en-us.14.0/JewelMenu1 
    http://g.msn.com/5meen_us/122 
    http://g.live.com/1rew3set/en_error?!draug_eciohc 
    http://g.live.com/1rew3set/en_ceip 
    http://g.live.com/1rew3set/en_toolbar但是不匹配:
    http://g.live.com/8SE.en-us.14.0/JewelMenu1.aspx 
    http://g.msn.com/5meen_us/122.htm 
    http://g.live.com/1rew3set/en_error?!draug_eciohc.xml 
    http://g.live.com/1rew3set/en_ceip.html 
    http://g.live.com/1rew3set/en_toolbar.aspx
      

  2.   

    在你的正则表达式最前面加上: 
    (?!.*\.(aspx?|html?|xml)$)
      

  3.   

    恩,不要误解贴的意思,后面跟随的不一定就是xml, aspx或者html,也可能是跟的其他的。
      

  4.   

       ?!(?=(aspx?|html?)$)加上这个
      

  5.   

    试试这个:
    @"(?in)^(?!.*\.[a-z]+$)(http|ftp|gopher|telnet|news)://(www\.)?([-a-z0-9/;?&=:_$+!*'(|\\~\[\]#%.])+$"
      

  6.   

    using System;
    using System.Text.RegularExpressions;class Program 

      static void Main() 
      {
        string[] array =
        {
    // 要求匹配: 
    "http://g.live.com/8SE.en-us.14.0/JewelMenu1",
    "http://g.msn.com/5meen_us/122",
    "http://g.live.com/1rew3set/en_error?!draug_eciohc",
    "http://g.live.com/1rew3set/en_ceip",
    "http://g.live.com/1rew3set/en_toolbar",// 但是不匹配: 
    "http://g.live.com/8SE.en-us.14.0/JewelMenu1.aspx",
    "http://g.msn.com/5meen_us/122.htm",
    "http://g.live.com/1rew3set/en_error?!draug_eciohc.xml",
    "http://g.live.com/1rew3set/en_ceip.html",
    "http://g.live.com/1rew3set/en_toolbar.aspx",
        };
        string patt = @"(?in)^(?!.*\.[a-z]+$)(http|ftp|gopher|telnet|news)://(www\.)?([-a-z0-9/;?&=:_$+!*'(|\\~\[\]#%.])+$";
        foreach (string s in array)
        {
          if (Regex.IsMatch(s, patt)) Console.WriteLine(s);
        }
      } 

    /* 程序输出:
    http://g.live.com/8SE.en-us.14.0/JewelMenu1
    http://g.msn.com/5meen_us/122
    http://g.live.com/1rew3set/en_error?!draug_eciohc
    http://g.live.com/1rew3set/en_ceip
    http://g.live.com/1rew3set/en_toolbar
    */
      

  7.   

    (?in) 其中的 i 表示不区分大小写的匹配, n 表示 () 是非捕获括号,省得每次都要写成 (?:)另外,在 [...] 当中, ?、$、+、*、(、)、|、. 都是普通字符,没有必要用 \ 转义。 - 表示 - 字符本身时要写在第一个,以避免被理解为区间。