速度-正确的正则表达式删除控制字符? [英] Velocity - Correct Regex to remove control characters?

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

问题描述

我正在尝试从Velocity中的字符串中删除不需要的字符(可以使用换行符,但不能使用EM和CAN ASCII控制字符之类的东西).

I'm trying to remove undesirable characters from a string in Velocity (newlines are ok, but not things like EM and CAN ASCII control characters).

#set($cleanScreen = $cleanScreen.replaceAll("\p{Cc}", ""))

投掷:

org.apache.velocity.exception.ParseErrorException: Lexical error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at line 13, column 82.  Encountered: "p" (112), after : "\"\\"
    at org.apache.velocity.Template.process(Template.java:137)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:415)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:335)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1102)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1077)
    at org.apache.velocity.runtime.RuntimeSingleton.getTemplate(RuntimeSingleton.java:303)
    at org.apache.velocity.app.Velocity.getTemplate(Velocity.java:503)

#set($cleanScreen = $cleanScreen.replaceAll("[[:cntrl:]]", ""))

这个不会引发异常,它会匹配字符c,n,t,r,l并将其从字符串中删除.

This one doesn't thrown an exception, instead, it matches the characters c,n,t,r,l and removes them from the string.

和...

#set($cleanScreen = $cleanScreen.replaceAll("\\p{Cntrl}", ""))

投掷:

java.util.regex.PatternSyntaxException: Illegal repetition near index 2
\\p{Cntrl}
  ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.closure(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor168.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:295)
    at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:245)

我已经尝试了几种正则表达式(很多似乎可以在Java中工作,但不能在VTL中工作)?我的关键问题似乎是Java和Velocity在转义上有什么不同?

I've tried several regex's (many seem to work in Java, but not VTL)? My key issue seems to be how things differ in their escaping between Java and Velocity?

任何人都可以帮忙吗?我只能访问VTL,而不能访问Java类.

Can anyone help? I only have access the the VTL, not the Java class.

推荐答案

我无法评论实际的正则表达式.

I can't comment on the actual regexp.

但是在速度方面,我发现...

On the velocity side however, I find that...

#set($cleanScreen = $cleanScreen.replaceAll("\p{Cc}", ""))
#set($cleanScreen = $cleanScreen.replaceAll("[[:cntrl:]]", ""))

...这两个是正确的.我有一个小的vtl shell,可以将您的vtl代码粘贴到其中.您是否真的通过前两个表达式得到了这些错误?怎么样使用'\ p {Cc}'?

...these two are correct as they are. I have a little vtl shell into which I just copy pasted your vtl code. Are you really getting these errors with the first two expressions? How about using '\p{Cc}'?

#set($cleanScreen = $cleanScreen.replaceAll("\\p{Cntrl}", ""))

"\\ p"使您陷入困境.

The '\\p' gets you into trouble.

另一方面,您可以使用 http://velocity.apache. org/tools/devel/generic/EscapeTool.html 满足您的所有转义需求.

On a side note, you can use http://velocity.apache.org/tools/devel/generic/EscapeTool.html for all your escaping needs.

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

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