【发布时间】:2009-09-03 15:16:36
【问题描述】:
我有一个 HTML 页面(它来自内部通讯簿应用程序),我正在尝试匹配表中的字段名称和字段值。
到目前为止,我编写的正则表达式是
"href.*?>(.*?)<\\/a.*>(.*?)<\\/span"
匹配大多数键和值就好了。问题是有些值也是链接。
示例字符串(无链接 - 有效)
href="JavaScript:updateField("peopleType", "390061", "bob.bobson@company.com", "bob", "Reg", "Bob Bobson");" onMouseOver="window.status='Update this field if possible, else explain how to update it';return true;" onMouseOut="window.status='';return true;">Emp Type</a></span></td>
<td nowrap=""><span style="font-family: Arial, Times New Roman, Courier New, Courier, monospace; color: #006699">Reg</span
示例字符串(带链接 - 不起作用)
href="JavaScript:updateField("dept", "390061", "bob.bobson@company.com", "bob", "Reg", "Bob Bobson");" onMouseOver="window.status='Update this field if possible, else explain how to update it';return true;" onMouseOut="window.status='';return true;">Dept</a></span></td>
<td nowrap=""><span style="font-family: Arial, Times New Roman, Courier New, Courier, monospace">
<a href="JavaScript:showDept('TheBobs');" onMouseOver="window.status='Show People in This Dept';return true;" onMouseOut="window.status='';return true;">TheBobs</a></span
前半部分(捕获密钥)正常工作。问题(似乎是)贪婪的 .* 一直匹配到链接的末尾,在该链接的末尾找到结束插入符,然后是非贪婪的.*?在捕获组中没有任何东西可以匹配。我尝试了正则表达式
"href.*?>(.*?)<\\/a.*>(.*?)(<\\/a>)?<\\/span"
它适用于带有链接的字符串(第三个捕获组 - 其中包含 /a)与链接的关闭匹配,因此我的第二个捕获组可以工作,但它不适用于以下值't 链接,因为(我认为)它正在搜索结束链接标签。我以为?在该捕获组的末尾应使其成为可选。
我正在匹配 RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace |正则表达式选项。单行。
如何让正则表达式匹配值中带有链接的大小写和不匹配的大小写? 谢谢。
【问题讨论】:
-
样本中的“字段名称”和“字段值”究竟应该是什么?