下面的Groovy命令说明了我的问题。
as seen on lotrepls.appspot.com
)如预期(注意
\u0061
是
'a'
).
>>> print "a".matches(/\u0061/)
true
现在假设我们想要匹配
\n
,使用Unicode转义
\u000A
. 以下,使用
"pattern"
作为字符串,行为与预期一致:
>>> 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
JLS 3.3
),所以:
print "\n".matches("\u000A")
实际上与:
print "\n".matches("
")
>>> print "\n".matches("\\u000A")
true
现在是问题部分:我们如何让它与Groovy一起工作
/pattern/
语法而不是使用字符串文字?
以下是一些失败的尝试:
>>> 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