<h3>数据库研究室</h3>
<ul>
<li><b>李波</b> [email protected]
<li><b>张鹏</b> [email protected]
...
</ul>
<h3>人工智能研究室</h3>
<ul>
<li><b>冯浩</b> [email protected]
<li><b>齐杰</b> [email protected]
...
<ul>
...
...一个网页的内容如上,如何用正则表达式检索出:姓名、电子邮件、所在实验室?
<ul>
<li><b>李波</b> [email protected]
<li><b>张鹏</b> [email protected]
...
</ul>
<h3>人工智能研究室</h3>
<ul>
<li><b>冯浩</b> [email protected]
<li><b>齐杰</b> [email protected]
...
<ul>
...
...一个网页的内容如上,如何用正则表达式检索出:姓名、电子邮件、所在实验室?
{
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> [email protected]
<li><b>张鹏</b> [email protected]
...
</ul>
<h3>人工智能研究室</h3>
<ul>
<li><b>冯浩</b> [email protected]
<li><b>齐杰</b> [email protected]
...
</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);
}
}
}
看来我接的不是时候……这里正则高手只有一个,过客,不过还没出现。真正是高手的原因是他的正则不局限于语言,而是真正的正则,java,php,js的都很精通,还有正则的实现原理等等。高手也不止过客,还有 思归,伴水,空军,南(009),坏(C++)都是高手。