【发布时间】:2015-02-02 21:10:10
【问题描述】:
我的代码不想工作。我想替换所有逗号,但替换这些逗号“,”和这个},{
import java.io.*;
public class converter {
public static void main(String[] args) throws IOException {
try {
BufferedReader br = new BufferedReader(
new FileReader("C:/Users/Sei_Erfolgreich/Desktop/convert.txt"));
String zeile;
try {
File newTextFile = new File("C:/Users/Sei_Erfolgreich/Desktop/convert2.txt");
FileWriter fw = new FileWriter(newTextFile);
while ((zeile = br.readLine()) != null) {
zeile = zeile.replaceAll("\",\"", "\uffff").replaceAll(",", "").replaceAll("\uffff", "\",\"")
.replaceAll("\uffff", "},{")
.replaceAll("},{", "\uffff");
System.out.println(zeile);
fw.write(zeile);
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
所以我想让这个逗号停留在},{ 之间,但它不起作用,我收到一条错误消息"Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 1 },{"
【问题讨论】:
-
不知道第二个 replaceAll,但第四个不会有太多可咀嚼的东西。
-
reaplceAll没有被调用,因为它替换了所有的出现;replace也是如此。之所以这样称呼它是因为它替换了所有匹配给定正则表达式的字符串。 -
请注意,您有 5 个
replaceAll实例。我怀疑你真的不想要几个。而且您确实应该将语句分解为分配给 temps 的多个语句——这样更容易调试,而且效率也不低。