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); }
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(); } } }
<a href=[\w\W]*?>[^<]*? </a >
提取中国用>[^<]*? </a >
然后再用Replace替换得到想要的结果
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);
}
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();
}
}
}
如何匹配包含'/hotelDetail.asp?ID=
或者'/hotelDetail.asp这样的<a href.....元素
<a\s+href\s*=\s*'/hotelDetail.asp(?ID=(? <ID >\d+)){0,1}'\s* >(?<text>[^ <]*)<\s*/a\s*>