【问题标题】:Second replaceALL not working第二个 replaceALL 不起作用
【发布时间】: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 的多个语句——这样更容易调试,而且效率也不低。

标签: java replace comma


【解决方案1】:

replaceAll() 方法采用正则表达式而不是正则字符串。 花括号在 Java 正则表达式中具有特殊含义

你必须转义花括号,否则它们将被理解为正则表达式的一部分。

但是下面的表达式将无法正常工作:

zeile.replaceAll("\",\"",  "\uffff")
     .replaceAll(",",      "")
     .replaceAll("\uffff", "\",\"")
     .replaceAll("\uffff", "},{")
     .replaceAll("},{",    "\uffff");

因为你多次调用replaceAll(),所以产生了多个中间结果,结果取决于之前的中间结果。

例如如果输入字符串的内容是",",},{,你的程序将会:

1. zeile.replaceAll("\",\"",  "\uffff")    // input = \uffff,},{
2.     .replaceAll(",",       "")          // input = \uffff}{
3.     .replaceAll("\uffff",  "\",\"")     // input = ","}{
4.     .replaceAll("\uffff",  "},{")       // input = ","}{
5.     .replaceAll("\\},\\{", "\uffff");   // input = ","}{

注意事项:

  • 请注意,第 1 行和第 3 行正在进行反向更改,首先将所有出现的 "," 替换为 \uffff,然后将它们全部替换回来
  • 第 4 行将永远不会执行,因为 \uffff 的所有出现都已在第 3 行中替换
  • 第 5 行也永远不会执行,因为 , 的所有出现都已在第 2 行中替换

【讨论】:

  • 我确实尝试过我自己的这种方式。replaceAll("\\},\\{", "\uffff"),但它仍在删除
【解决方案2】:

字符{} 在正则表达式(量词)中具有特殊含义。使用\ 转义这些字符。

使用第二个 \ 为 Java 转义。

结果:.replaceAll("\\},\\{", "\\uffff");

更新

如下更改替换顺序:

       zeile = zeile.replaceAll("\",\"", "\uffff")    //backup any "," 
                    .replaceAll("\\},\\{", "\ueeee")  //backup any },{
                    .replaceAll(",", "")              //replace all commas that are still in the string
                    .replaceAll("\uffff", "\",\"")    //restore ","
                    .replaceAll("\ueeee", "},{");     //restore },{

【讨论】:

  • 有解决办法,但是不能发^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 2019-02-02
  • 1970-01-01
相关资源
最近更新 更多