Velocity 模板 - 正则表达式

发布于 2024-11-09 10:45:37 字数 578 浏览 4 评论 0原文

我刚刚开始在新工作中使用速度,但我真的不喜欢我已经发现的东西。在这一点上,我实际上更喜欢 freemarker =X。 不管怎样,我正在尝试做一个正则表达式,我看到了 这一点(搜索“regular表达式”),但这并不完全是我正在做的实现。

我没有任何对java的访问权限,因此不存在编写自定义内容来执行此操作的选项(我什至不确定这是否可能)。

这就是我现在所拥有的:

#set ( $envCheck = "(localhost|staging|qa|cms)\\.site" )
#set ( $envCheck = $envCheck.matches($gatewayURL) )

但是 $envCheck 总是显示为“false”。 $gatewayURL 也在页面上定义,所以这不是问题。

这可能吗?我读到 java String 类具有的任何正则表达式方法都可以在速度模板中使用。

I just started using velocity for a new job and I really don't like what I have found already. At this point, i would actually prefer freemarker =X.
Anyways, i'm trying to do a regular expression and i saw this little bit (search "regular expression"), but that isn't quite the implementation I am doing.

I do not have any access to the java so that option of writing something custom to do this stuff is not there (i'm not even sure if that is possible).

This is what i have right now:

#set ( $envCheck = "(localhost|staging|qa|cms)\\.site" )
#set ( $envCheck = $envCheck.matches($gatewayURL) )

but $envCheck always just comes out as "false". $gatewayURL is defined on the page as well, so that is not the issue.

is this even possible? i was reading that any regexp method that the java String class has is available in the velocity template.

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

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

发布评论

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

评论(2

何止钟意 2024-11-16 10:45:37

假设您的 $gatewayURL 是这样的:

#set ( $gatewayURL = "localhost.site" )

那么:

#set ( $envCheck = "(localhost|staging|qa|cms)\.site" )
#set ( $envCheck = $gatewayURL.matches($envCheck) )

不需要屏蔽反斜杠,并且您应该在 gatewayURL 上调用 matches(),而不是正则表达式。

Velocity 没有自己的正则表达式实现,它只是将您提供的参数传递给相应的本机 java 方法,仅此而已。因此,您拥有几乎完整的 Java SDK 可供使用。

Assuming your $gatewayURL is somethign like this:

#set ( $gatewayURL = "localhost.site" )

Then:

#set ( $envCheck = "(localhost|staging|qa|cms)\.site" )
#set ( $envCheck = $gatewayURL.matches($envCheck) )

No need to mask backslash, and you should be calling matches() on gatewayURL, not regular expression.

Velocity doesn't have its own regexp implementation, it just passes parameters you provide to corresponding native java methods, that's all. So you have pretty much full Java SDK at your disposal.

浅忆 2024-11-16 10:45:37

这个答案已经很晚了,但对于遇到同样问题的 Velocity 用户来说可能仍然是一个很好的参考。

我们使用 Velocity 1.5(升级到 1.7/1.6 的任务太大,因为它们破坏了太多模板)并遇到了同样的问题。上面的答案不起作用 - 没有转义(\)的反冲会导致词法错误,而转义(\\)总是返回 false,因为我认为它是按字面解释的。解决这个问题的正确方法是在定义正则表达式时使用单引号而不是双引号,这样 Velocity 就不会尝试解释适用于 Java 的字符串。

#set ( $envCheck = '(localhost|staging|qa|cms)\.site' )
#set ( $envCheck = $envCheck.matches($gatewayURL) )

This answer is way late but probably still good as a reference for Velocity users encountering the same issue.

We use Velocity 1.5 (too big a task to upgrade to 1.7/1.6 as they broke too many templates) and encountered the same issue. The answer above would not work - backlash without escape (\) results in Lexical error and with escape (\\) return false always as I think it is being interpreted literally. The right way to solve it is by using single quote instead of double quotes when defining the regex expression so Velocity would not attempt to interpret the string that is meant for Java.

#set ( $envCheck = '(localhost|staging|qa|cms)\.site' )
#set ( $envCheck = $envCheck.matches($gatewayURL) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文