我在使用 Java 从 html 表中提取信息的程序时遇到了一些问题。 要从每一列中提取信息,我使用以下 RegEx:
<td>([^<]*)</td>
这对我来说非常好。 为了获取链接名,我使用了这个:
<a[^>]*>(.*?)</a>
这也很好用。 但有时我需要来自链接所在列的信息。因此我想将这些正则表达式与:
<td>([^<]*)</td>|<a[^>]*>(.*?)</a>
我认为它会像这样工作:
它获取
<td>
之间的所有内容和</td>
如果是链接,它也只获取链接名
但这行不通。我不是最擅长 RegEx 的,所以我需要帮助来结合这两个步骤。
非常非常感谢。
最佳答案
我使用的代码:
Pattern pattern = Pattern.compile("<td>([^<]*)</td>|<a[^>]*>(.*?)</a>");
String line = "Here are the lines saved from the HTML downloader";
Matcher matcher = pattern.matcher(line);
for (int startPoint = 0; matcher.find(startPoint); startPoint = matcher.end())
{
System.out.prinln(matcher.group(1));
}
这只是一个片段 - 但它通常是这样工作的。 (通常将字符串保存在一个数组中)。
关于java - 在 Java 中组合正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26722091/