62,046
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}
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);
}