111,095
社区成员




public class Employee
{
public string Name { get; set; }
public string Email { get; set; }
}
private static void TestRegex10()
{
Regex reg1 = new Regex(@"(?is)<h3>(?<yjs>.+?)</h3>.*?<ul>.+?</ul>");
Regex reg2 = new Regex(@"(?im)<li><b>(?<name>.+?)</b>\s*(?<email>.+)");
string yourStr = @"<h3>数据库研究室</h3>
<ul>
<li><b>李波</b> libo@gmail.com
<li><b>张鹏</b> zhangpeng@hotmail.com
...
</ul>
<h3>人工智能研究室</h3>
<ul>
<li><b>冯浩</b> fenghao@126.com
<li><b>齐杰</b> qijie@126.com
...
</ul>
...
...
";
Dictionary<string, List<Employee>> dict = new Dictionary<string, List<Employee>>();
MatchCollection mc1 = reg1.Matches(yourStr);
foreach (Match m1 in mc1)
{
List<Employee> list = new List<Employee>();
MatchCollection mc2 = reg2.Matches(m1.Value);
foreach (Match m2 in mc2)
{
Employee e = new Employee();
e.Name = m2.Groups["name"].Value;
e.Email = m2.Groups["email"].Value;
list.Add(e);
}
dict.Add(m1.Groups["yjs"].Value, list);
}
//输出
foreach (string key in dict.Keys)
{
Console.WriteLine("研究室:" + key);
foreach (Employee e in dict[key])
{
Console.WriteLine("姓名:" + e.Name + " email:" + e.Email);
}
}
}