2011-10-23 6 views
1

Matcher.replaceAll() (즉 Matcher.replaceAll("$2")).여러 역 참조()을 <strong>하나의</strong><a href="http://www.regular-expressions.info/java.html" rel="nofollow">backreference</a> 작품 중대한와

하지만 두 가지 이상의 역 참조로 작동하지 못했습니다. Matcher.replaceAll("$1$2").

Matcher.replaceAll()은 여러 개의 하위 참조를 모두 지원합니까? 그렇다면, 그것을 사용하기위한 적절한 구문은 무엇입니까?

+2

Matcher.replaceAll ("$ 1 $ 2") 작동합니다. 뭔가 잘못하고 있습니다. –

+1

http://download.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#replaceAll(java.lang.String) 이상한 점은 없습니다. 아마도 두 번째 역 참조가 비어있을 것입니까? – FailedDev

답변

3

잘 작동합니다. 다음 코드 :

Pattern p = Pattern.compile("(.)(.)"); 
Matcher m = p.matcher("ab"); 
System.out.println(m.replaceAll("$2$1")); 

인쇄됩니다

ba 
+0

모두 틀립니다. 내가 사용한 구문이 맞았지만'Matcher.replaceAll ("$ 1 $ 2")'의 반환 값을 출력 문자열에 할당하지 못했습니다. 모든 것이 잘되었습니다. 감사! – uTubeFan