Velocity:查找数学字符串并将它们存储在数组中

发布于 2024-12-21 03:30:57 字数 295 浏览 3 评论 0原文

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

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

How can I store all the events between

and
在数组中避免空/空格字符串?

谢谢。

I have a text and I want to extract some string basing on a regular expression pattern:

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

How can I store all the occurences between <div> and </div> in an array avoiding the empty/space strings ?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2024-12-28 03:30:57

您似乎想要解析 html/xml 文档中的内容。 Velocity 对于获取字符串数组并将它们放入 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));
}

It seems that you want to parse content from an html/xml document. Velocity would be useful for taking an array of strings and putting them in div tags... not the other way around.

HtmlCleaner is a useful tool which formats html into xml (i.e. includes closing p tags and things). You can then easily get the content of the div tags using xpath.

Here's some untested code that should get you started:

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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文