Velocity:查找匹配的字符串并将其存储在数组中

时间:2011-12-12 12:39:34

标签: string velocity

我有一个文本,我想根据正则表达式模式提取一些字符串:

<div>This is a text</div><div>  </div><div>here is another text</div>

如何将<div></div>之间的所有出现存储在一个避免空/空格字符串的数组中?

谢谢。

1 个答案:

答案 0 :(得分:0)

您似乎想要解析html / xml文档中的内容。速度对于获取一个字符串数组并将它们放在div标签中非常有用......而不是相反。

HtmlCleaner是一个有用的工具,它将html格式化为xml(即包括关闭p标签和东西)。然后,您可以使用xpath轻松获取div标记的内容。

这是一些可以帮助您入门的未经测试的代码:

try {
    HtmlCleaner cleaner = new HtmlCleaner();
    TagNode node = cleaner.clean(htmlString);
    Object[] elements = node.evaluateXPath("//div");
    for(Object element : elements){
        System.out.println(((TagNode) element).getText().toString());
    }
} catch (IOException e) {
    Logger.getLogger().error(ExceptionUtils.getStackTrace(e));
} catch (XPatherException e) {
    Logger.getLogger().error(ExceptionUtils.getStackTrace(e));
}
相关问题