我有一个正在迭代的运算符字符串数组。当我找到一个存在于 中的strLine
,我用一个空白字符串替换它的第一个实例。当我到达时{
,我得到了java.util.regex.PatternSyntaxException: Illegal repetition
。
现在,我知道这{
是一个特殊的 java 运算符,所以这就是它失败的原因。用我现在的设置来逃避这个角色的最好方法是什么?
String[] operators = {".", ",", "{", "}", "!", "++", "--", "*", "/", "%", "+", "-", "<"}
String strLine = "for (int count = input.length(); count > 0; count--) {";
strLine = strLine.trim();
for (int i = 0; i < operators.length; i++) {
if(strLine.contains(operators[i])) {
strLine = strLine.replaceFirst(operators[i]+"\\s*", "");
System.out.println("Removal of: " + operators[i]);
System.out.println("Sentence after removal: " + strLine);
}
}