使用Java解析文件并替换双引号内的空格

时间:2015-08-06 16:56:04

标签: java string bufferedreader

我正在阅读文件并尝试按以下顺序修改它:

  1. 如果行为空trim()
  2. 如果行结束\剥离该char并添加下一行。
  3. 完整的行包含双引号,引号之间有空格,用~替换空格。    例如:"This is text within double quotes"    更改为:"This~is~text~within~double~quotes"
  4. 此代码正在运行但有问题。 当找到以\结尾的行和其他完成的行时,会出现问题。

    for example: 
    line 1 and \
    line 2
    line 3
    

    所以没有

    line 1 and line 2
    line 3
    

    我有这个:

    line 1 and line 2     line 3
    

    编码更新:

    public List<String> OpenFile() throws IOException {
            try (BufferedReader br = new BufferedReader(new FileReader(path))) {
                String line;
                //StringBuilder concatenatedLine = new StringBuilder();
                List<String> formattedStrings = new ArrayList<>();
                //Pattern matcher = Pattern.compile("\"([^\"]+)\"");
            while ((line = br.readLine()) != null) {
                boolean addToPreviousLine;
                if (line.isEmpty()) {
                    line.trim();
    
                }
    
                if (line.contains("\"")) {
                    Matcher matcher = Pattern.compile("\"([^\"]+)\"").matcher(line);
                    while (matcher.find()) {
                        String match = matcher.group();
                        line = line.replace(match, match.replaceAll("\\s+", "~"));
                    }
    
                }
    
                if (line.endsWith("\\")) {
                    addToPreviousLine = false;
                    line = line.substring(0, line.length() - 1);
                    formattedStrings.add(line);
                } else {
                    addToPreviousLine = true;
    
                }
    
                if (addToPreviousLine) {
                    int previousLineIndex = formattedStrings.size() - 1;
                    if (previousLineIndex > -1) {
                        // Combine the previous line and current line
                        String previousLine = formattedStrings.remove(previousLineIndex);
                        line = previousLine + " " + line;
                        formattedStrings.add(line);
                    }
                }
                testScan(formattedStrings);
                //concatenatedLine.setLength(0);
            }
            return formattedStrings;
        }
    

5 个答案:

答案 0 :(得分:1)

更新

我正在给你你需要的东西,而不是试图为你编写所有的代码。你只需要弄清楚这些片段的放置位置。

如果line为空trim()

if (line.matches("\\s+")) {
    line = "";
    // I don't think you want to add an empty line to your return result.  If you do, just omit the continue;
    continue;
}

如果line中包含双引号和空格,请用〜替换空格。例如:"This is text within double quotes"更改为:"This~is~text~within~double~quotes"

Matcher matcher = Pattern.compile("\"([^\"]+)\"").matcher(line);
while (matcher.find()) {
    String match = matcher.group();
    line = line.replace(match, match.replaceAll("\\s+", "~"));
}

如果行以\结束,则删除该字符并添加下一行。您需要有标记来跟踪何时执行此操作。

if (line.endsWith("\\")) {
    addToPreviousLine = true;
    line = line.substring(0, line.length() - 1);
} else {
    addToPreviousLine = false;
}

现在,要将下一行添加到上一行,您需要类似的内容(找出放置此代码段的位置):

if (addToPreviousLine) {
    int previousLineIndex = formattedStrings.size() - 1;
    if (previousLineIndex > -1) {
        // Combine the previous line and current line
        String previousLine = formattedStrings.remove(previousLineIndex);
        line = previousLine + " " + line;
    }
}

您仍然不需要StringBufferStringBuilder。只需修改当前行,然后将当前行添加到formattedStrings List

答案 1 :(得分:0)

我对正则表达式不太了解,所以这是一种编程方法:

    String string = "He said, \"Hello Mr Nice Guy\"";

    // split it along the quotes
    String splitString[] = string.split("\"");

    // loop through, each odd indexted item is inside quotations
    for(int i = 0; i < splitString.length; i++) {
        if(i % 2 > 0) {
            splitString[i] = splitString[i].replaceAll(" ", "~");
        }
    }

    String finalString = "";

    // re-build the string w/ quotes added back in
    for(int i = 0; i < splitString.length; i++) {
        if(i % 2 > 0) {
            finalString += "\"" + splitString[i] + "\"";
        } else {
            finalString += splitString[i];
        }
    }

    System.out.println(finalString);

输出:他说,&#34;你好〜先生〜尼斯〜盖伊&#34;

答案 2 :(得分:0)

而不是

    $('.sticky').css('margin-top',$('.site-header').outerHeight()).scrollToFixed();

我会这样做

if (line.contains("\"")) {
                   StringBuffer sb = new StringBuffer();
                   Matcher matcher = Pattern.compile("\"([^\"]+)\"").matcher(line);
                   while (matcher.find()) {
                   matcher.appendReplacement(sb, matcher.group().replaceAll("\\s+", ""));
                }

如何将其添加到上面的内容中?

if (line.matches(("\"([^\"]+)\"")) {
               line=   line.replaceAll("\\s+", ""));
                }

答案 3 :(得分:0)

第3步:

String text;
text = text.replaceAll("\\s", "~");

答案 4 :(得分:0)

如果要用~ s的双引号替换空格,

if (line.contains("\"")) {
    String line = "\"This is a line with spaces\"";
    String result = "";
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m = p.matcher(line);
    while (m.find()) {
          result = m.group(1).replace(" ", "~");
    }
}