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