110,533
社区成员
发帖
与我相关
我的任务
分享
string str = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\test.txt", System.Text.Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)<a[^>]*?>[^<>]*?2[^>]*?>");
foreach (Match m in reg.Matches(str))
{
Response.Write(m.Value + "<br/>");
}
string text = "<a>abc</a><a><a>123</a><a>456</a><a>789</a></a>";
MatchCollection matchs = Regex.Matches(text, @"<a>(?<data>\d*[2]+\d*[^</a>]*)</a>");
foreach (Match mh in matchs)
Console.WriteLine(string.Concat("regex:", mh.Groups[0]," Value:", mh.Groups["data"]));
string tempStr = @"<a>abc</a><a><a>123</a><a>456</a><a>789</a></a>";
string keyword = "2";
string pattern = @"<a[^>]*>[^<]+"+keyword+"[^<]*</a>";
foreach (Match m in Regex.Matches(tempStr,pattern))
{
//循环输出
string value = m.Value;
//<a>123</a>
}
string str = "<a>abc</a><a><a>123</a><a>456</a><a>789</a></a>";
Regex reg = new Regex("<a>(?:(?!</?a).)*2(?:(?!</?a).)*</a>");
Console.WriteLine(reg.Match(str).Value);
<a>abc</a><a><a>123</a><a>456</a><a>789</a></a>
如果要匹配其中的"<a>123</a>",我该怎么做呢?比如"2"是关键字.楼上的话应该匹配的是"<a><a>123</a>"吧.还请大家点拨一下.
string str = @"<table>热点资讯</table><table></table>";
string pattern = "<table[^>]*?>(?<keyword>.*?)</table>";
Regex regex = new Regex(pattern);
MatchCollection mc = regex.Matches(str);
foreach (Match m in mc)
Console.WriteLine(m.Groups["keyword"].Value);
Console.ReadKey();