Programming/Etc
자바 replaceAll 괄호 오류 : Unclosed character class near index 0, java.util.regex.PatternSyntaxException: Illegal repetition
감성적인 개발자
2022. 10. 20. 23:04
오류메세지
Unclosed character class near index 0 [
java.util.regex.PatternSyntaxException: Illegal repetition {
String java.lang.String.replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with thegiven replacement.
replaceAll 메서드가 문자열을 치환할 때 regex(정규표현식)을 이용하기 때문에 발생하는 오류로 괄호 앞에 \\를 붙여 치환해주면 된다.
string1 = string1.replaceAll("[", ""); //오류
string1 = string1.replaceAll("\\[", ""); //수정
string2 = string2.replaceAll("{", ""); //오류
string2 = string2.replaceAll("\\{", ""); //수정