2010-07-13 4 views
2

다음 Groovy 명령은 내 문제를 설명합니다.Groovy의/pattern/구문에서 유니 코드 이스케이프를 이스케이프 처리하는 방법

우선,이 작업은 (as seen on lotrepls.appspot.com) 예상대로 (\u0061'a' 임).

>>> print "a".matches(/\u0061/) 

true 

이제 우리는 유니 코드 이스케이프 \u000A를 사용하여, \n을 일치시킬 것을 가정 해 봅시다. 문자열로 "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 

답변

0

~ "[\ u0000- \ u0008 \ u000B \ u000C \ u000E- \ u001F \ u007F- \이 u009F]"

과 같이 일하게 나타난다 그것은해야한다. 필자가 본 문서에 따르면 이중 슬래시는 슬래시 문자열과 함께 사용하면 안되기 때문에 컴파일러가 왜 만족스럽지 않은지 알 수 없습니다.

+0

lotrepl.appspot.com은 '\ u0000'에 완벽하게 만족합니다 (http://stackoverflow.com/questions/3240356/groovy-regex-problem/3242087#3242087 참조). – polygenelubricants

관련 문제