如何在Groovy的/pattern/语法中转义Unicode转义 [英] How to escape Unicode escapes in Groovy's /pattern/ syntax
问题描述
以下Groovy命令说明了我的问题.
The following Groovy commands illustrate my problem.
First of all, this works (as seen on lotrepls.appspot.com) as expected (note that \u0061
is 'a'
).
>>> print "a".matches(/\u0061/)
true
现在,我们要使用Unicode转义 \ u000A
匹配 \ n
.以下使用"pattern"
作为字符串,其行为符合预期:
Now let's say that we want to match \n
, using the Unicode escape \u000A
. The following, using "pattern"
as a string, behaves as expected:
>>> print "\n".matches("\u000A");
Interpreter exception: com.google.lotrepls.shared.InterpreterException:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed,
Script1.groovy: 1: expecting anything but ''\n''; got it anyway
@ line 1, column 21. 1 error
这是可以预期的,因为至少在Java中,Unicode转义会及早处理( JLS 3.3 ),因此:
This is expected because in Java at least, Unicode escapes are processed early (JLS 3.3), so:
print "\n".matches("\u000A")
确实与:
print "\n".matches("
")
解决方法是对Unicode转义进行转义,然后让正则表达式引擎对其进行处理,如下所示:
The fix is to escape the Unicode escape, and let the regex engine process it, as follows:
>>> print "\n".matches("\\u000A")
true
现在这是有问题的部分:如何使它与Groovy /pattern/
语法一起使用,而不是使用字符串文字?
Now here's the question part: how can we get this to work with the Groovy /pattern/
syntax instead of using string literal?
以下是一些失败的尝试:
Here are some failed attempts:
>>> print "\n".matches(/\u000A/)
Interpreter exception: com.google.lotrepls.shared.InterpreterException:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed,
Script1.groovy: 1: expecting EOF, found '(' @ line 1, column 19.
1 error
>>> print "\n".matches(/\\u000A/)
false
>>> print "\\u000A".matches(/\\u000A/);
true
推荐答案
〜"[[\ u0000- \ u0008 \ u000B \ u000C \ u000E- \ u001F \ u007F- \ u009F]"
~"[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]"
似乎可以正常工作.根据我所看到的文档,斜线字符串不需要双反斜杠,所以我不知道为什么编译器对它们不满意.
Appears to be working as it should. According to the docs I've seen, the double backslashes shouldn't be required with a slashy string, so I don't know why the compiler's not happy with them.
这篇关于如何在Groovy的/pattern/语法中转义Unicode转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!