我正在编写一个函数来删除转义字符,它把我扔了PatternSyntaxException
。我在这里想念什么?我将该函数编写为单个应用程序,尽管如此,我还是收到了这个错误。
public class test {
public static void main(String a[])
{
String keyword = "otterbox 3500 series { { waterproof case \\(clear\\) phones";
if(keyword.contains("\\")){
int l=0;
int l2=0;
for (int i = 0; i < keyword.length(); i++){
char c = keyword.charAt(i);
if(c=='\\')l++;
if (c == '!' || c == '(' || c == ')' || c == '&' ||
c == ':' || c == '^' || c == '[' || c == ']' || c=='-'||
c == '{' || c == '}' || c == '~'){
keyword =keyword.replaceAll("\\\\"+c, ""+c);
l2++;
}
}
if(l==1) keyword= keyword.replaceAll("\\\\", "");
if(l>1 && l2==1) keyword = keyword.replaceFirst("\\\\", "");
}
System.out.println("Final "+keyword);
}}
它向我抛出了以下异常:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 1
{
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.closure(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at test.main(test.java:21)
}