Exception java.lang.IllegalArgumentException

황제낙엽 2010.01.18 16:58 조회 수 : 130513 추천: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 130513
350 Using RSS in JSP pages (Informa Project) 황제낙엽 2006.01.10 37829
349 JSP 파일에서 getOutputStream() has already been called for this response 에러 황제낙엽 2013.04.24 11479
348 한글 파일명 깨짐으로 살펴본 다국어 처리 문제 (UTF-8) 황제낙엽 2012.03.22 10121
347 세션의 timeout 설정 >> HttpSession.setMaxInactiveInterval() 황제낙엽 2019.07.03 8311
346 [JSON기초04] 자바 JSON 데이터에서 KEY 값 알아오기 (TIP) 황제낙엽 2017.01.18 6641
345 java.util.Queue file 황제낙엽 2022.04.06 5382
344 쓰레드(Thread)를 중간에 종료시키는 방법 황제낙엽 2017.03.15 5127
343 Java 실행 옵션들 황제낙엽 2017.08.23 3367
342 일본어 전각 반각 변환 예제 소스 .첫번째 file 황제낙엽 2007.01.10 3070
341 byte배열에 대한 CRC 를 계산하는 메서드 (java.util.zip.CRC32) 황제낙엽 2010.03.14 2166
340 UTF형태 파일에서 BOM 제거하기 황제낙엽 2008.06.16 1938
339 File.delete() 와 File.deleteOnExit() 황제낙엽 2019.03.24 1887
338 [대용량 파일 업로드] multipart form parser - http file upload, database 저장 java class 연재2 file 황제낙엽 2009.06.19 1831
337 싱글톤 방식의 테스트용 Temporary Data Access Object 황제낙엽 2017.01.12 1603
336 servlet 에서의 json 한글처리 황제낙엽 2013.04.23 1519
335 날짜, 시간 문자열 값으로 Date 오브젝트로 만들기 >> SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US) 황제낙엽 2017.10.31 1516
334 J2EE object-caching frameworks (ObjectCache) 황제낙엽 2007.11.02 1495
333 [대용량 파일 업로드] multipart form parser - http file upload 기능 java class 연재1 file 황제낙엽 2009.06.19 1436
332 JavaMail - 네이버 메일 수신하기(POP3) 황제낙엽 2018.08.20 1413