Exception java.lang.IllegalArgumentException

황제낙엽 2010.01.18 16:58 조회 수 : 140554 추천:134

sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
        at java.util.regex.Matcher.appendReplacement(Matcher.java:561)
        at java.util.regex.Matcher.replaceAll(Matcher.java:661)
        at java.lang.String.replaceAll(String.java:1663)



내용 보기

java.lang.IllegalArgumentException: Illegal group reference, replaceAll and dollar signs This weblog is officially about inane things I run into while trying to do my job at work. Let's say you have a String object like this:

String mystring = "Your password: #PASSWORD";

and at runtime you need to replace the value of #PASSWORD with a password that a user typed in. You'd write something like this:

String password = "$Jslwe"
mystring = mystring.replaceAll("#PASSWORD", password);

What would happen? You'd expect that the key #PASSWORD would get replaced with the value of the variable 'password' (which is "$Jslwe") and then you'd move happily on your way to something much more interesting. But no, Java throws you an error:

java.lang.IllegalArgumentException: Illegal group reference

which is extremely helpful. Turns out that the second argument to the String replaceAll method "may" have some issues with dollar signs and backslashes which you only find out about if you dig into the Matcher class that backs the replaceAll method or if you're lucky and you read about the whole thing on a site devoted to regular expressions. In short:


myString.replaceAll("regex", "replacement") replaces all regex matches inside the string with the replacement string you specified. No surprises here. All parts of the string that match the regex are replaced. You can use the contents of capturing parentheses in the replacement text via $1, $2, $3, etc. $0 (dollar zero) inserts the entire regex match. $12 is replaced with the 12th backreference if it exists, or with the 1st backreference followed by the literal "2" if there are less than 12 backreferences. If there are 12 or more backreferences, it is not possible to insert the first backreference immediately followed by the literal "2" in the replacement text.

In the replacement text, a dollar sign not followed by a digit causes an IllegalArgumentException to be thrown. If there are less than 9 backreferences, a dollar sign followed by a digit greater than the number of backreferences throws an IndexOutOfBoundsException. So be careful if the replacement string is a user-specified string. To insert a dollar sign as literal text, use $ in the replacement text. When coding the replacement text as a literal string in your source code, remember that the backslash itself must be escaped too: $.

String.replaceAll() 의 두번째 인자값(문자열)에 '$' 가 존재하면 발생하는 Exception 이다.

replaceAll() 하기 전에 두번째 문자열을 다음과 같이 강제로 처리해 준다.

str.replaceAll("$", "\$");

번호 제목 글쓴이 날짜 조회 수
» java.lang.IllegalArgumentException 황제낙엽 2010.01.18 140554
166 org.apache.commons.fileupload.servlet.ServletFileUpload 를 이용한 파일 업로드 file 황제낙엽 2009.11.19 1770
165 Error reading tld listeners java.lang.NullPointerException 황제낙엽 2009.10.14 1766
164 Cannot find the tag library descriptor for “http://java.sun.com/jsp/jstl/core 황제낙엽 2009.10.14 2591
163 Transfer-Encoding: chunked VS Content-Length 황제낙엽 2009.09.17 1768
162 서블릿 응답 헤더(Response Header) 황제낙엽 2009.09.17 1704
161 같은 문자열인데도 정규식에서 해당 문자열을 파싱하지 못하는 경우 황제낙엽 2009.08.08 1761
160 MultipartRequest (cos.jar)와 서블릿을 이용한 업로드 file 황제낙엽 2009.06.19 1951
159 [대용량 파일 업로드] multipart form parser - http file upload, database 저장 java class 연재2 file 황제낙엽 2009.06.19 3551
158 [대용량 파일 업로드] multipart form parser - http file upload 기능 java class 연재1 file 황제낙엽 2009.06.19 2854
157 [reflection/리플렉션] Class.forName 황제낙엽 2009.05.27 1790
156 문자열 내의 공백을 제거하는 간단한 정규식 황제낙엽 2009.05.20 1765
155 문자열에서 특수 문자 (Escape Sequence) 처리 file 황제낙엽 2009.02.20 2922
154 정규표현식을 사용하는 String클래스의 replaceAll() 함수 개량 황제낙엽 2009.02.09 1886
153 File 복사 함수 황제낙엽 2009.02.08 1715
152 JSP session 정보 얻기 황제낙엽 2009.01.21 1729
151 서버상의 로컬경로 (실제경로) 관련 환경변수 황제낙엽 2009.01.21 1958
150 java.net.URL 생성시 로컬 파일에 접근 황제낙엽 2009.01.20 1669
149 자바로 구현하는 Web-to-web 프로그래밍 황제낙엽 2009.01.20 1735
148 Using Java's Net::URL Class To Access URLs (java.net.URL) 황제낙엽 2009.01.20 2100