string text = "I'vefoundthisamazingURLathttp://www.sohu.com,andthenfindftp://ftp.sohu.comisbetter.";
            string pattern = @"b(?<protocol>S+)://(?<address>S+)b";
            MatchCollection mc = Regex.Matches(text,pattern);
            Response.Write("文本中包含的URL地址有:");
            foreach(Match match in mc)
            {
                GroupCollection gc = match.Groups;
                string outputText = "URL:" + match.Value + ";Protocol:" + gc["protocol"].Value + ";Address:" + gc["address"].Value;
                Response.Write(outputText);
            }输出为空,我想输出
http://www.sohu.com
ftp://ftp.sohu.com  哪里不对了呢???

解决方案 »

  1.   


    string text = "I'vefoundthisamazingURLathttp://www.sohu.com,andthenfindftp://ftp.sohu.comisbetter.";
                System.Text.RegularExpressions.Regex   reg= new  System.Text.RegularExpressions.Regex(@"(http|ftp)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase);
               MatchCollection mc = reg.Matches(text); ;
                Console.WriteLine("文本中包含的URL地址有:");
                foreach (Match match in mc)
                {
                    string url = match.Value;
                    string outputText = "URL:" + url.Substring(url.IndexOf("//")+2) +";Protocol:" + url.Substring(0, url.IndexOf(":"));
                    Console.WriteLine(outputText);
                }
                Console.ReadLine();
      

  2.   


    if(!Regex.Match(text,@"b(?<protocol>S+)://(?<address>S+)b").Success)
    {
    ...
    }
    )