Velocity Template - regular expressions

岁酱吖の 提交于 2019-12-01 02:01:20
serg

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.

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) )
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!