求c#正则:
<a href='/hotelDetail.asp?ID=4598'>中国</a>
能够取ID的值 及能取到“中国”、。

解决方案 »

  1.   

    先用
    <a href=[\w\W]*?>[^<]*? </a > 
    提取中国用>[^<]*? </a > 
    然后再用Replace替换得到想要的结果 
      

  2.   

    string test = "<a href='/hotelDetail.asp?ID=4598'>中国</a>";
    Match m = Regex.Match(test, @"<a\s+href='[^=]*=(?<ID>\d+)'\s*>(?<text>[^<]*)</a>", RegexOptions.IgnoreCase);
    if (m.Success)
    {
        MessageBox.Show(m.Groups["ID"].Value);
        MessageBox.Show(m.Groups["text"].Value);
    }
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;namespace regtest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Regex re = new Regex("<a href='[\\w|.|/]+\\?ID=(\\d+)'>(\\w+)</a>", RegexOptions.None);
                MatchCollection mc = re.Matches("<a href='/hotelDetail.asp?ID=4598'>中国</a>\r\n<a href='/hotelDetail.asp?ID=4599'>英国</a>");
                foreach (Match ma in mc)
                {
                    Console.Write("ID={0},text={1}\n", ma.Groups[1], ma.Groups[2]);
                }
                Console.Read();
            }
        }
    }
      

  4.   

    <a\s*href='[^=]*=(?<ID>\d+)'\s*>(?<text>[^<]*)</a>
      

  5.   

    to:whislly
    如何匹配包含'/hotelDetail.asp?ID=
    或者'/hotelDetail.asp这样的<a href.....元素
      

  6.   

    你只要/hotelDetail.asp这样的<a href.....元素吗?
    <a\s+href\s*=\s*'/hotelDetail.asp(?ID=(? <ID >\d+)){0,1}'\s* >(?<text>[^ <]*)<\s*/a\s*>