速度模板 - 正则表达式 [英] Velocity Template - regular expressions

查看:51
本文介绍了速度模板 - 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在一份新工作中使用 Velocity,但我真的不喜欢我已经找到的东西.在这一点上,我实际上更喜欢 freemarker =X.无论如何,我正在尝试做一个正则表达式,我看到了 这一点(搜索regular表达式"),但这并不是我正在做的实现.

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.

我无法访问 Java,因此不存在编写自定义内容来执行此操作的选项(我什至不确定这是否可行).

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) )

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

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

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

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

推荐答案

这个答案来得太晚了,但对于遇到相同问题的 Velocity 用户来说,它可能仍然是一个很好的参考.

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

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

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) )

这篇关于速度模板 - 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆