首页 > 解决方案 > Velocity 中的 Regex 和 ReplaceAll 提取文本

问题描述

在速度我有:

#set( $text = "#parse('file url')" )

我想要的是只提取名为“paragrafo-html”的 div 内的文本。

文件内容和正则表达式在这里:

https://regex101.com/r/wr8H04/1

我写过:

$text.replaceAll('(?:.|\\n)*?<div class="paragrafo-html" style="">((?:.|\\n)*?)</div>(?:.|\\n)*', '$1')

但它不起作用,而正则表达式组在我看来是正确的。

还:

$text.matches('(?:.|\n)*?<div class="paragrafo-html" style="">(?:.|\n)*?</div>(?:.|\n)*')

给我错误。

你能建议我在replaceAll中做错了什么吗?

标签: regexvelocity

解决方案


Wiktor Stribizew 最后的评论有效!所以:

$text.replaceAll('(?s).*?<div\s+class="paragrafo-html"\s+style="">(.*?)</div>.*', '$1')

谢谢!


推荐阅读