如何根据keyword/pattern将一行文本拆分为多行?

How to split one line of text to multiple lines based on keyword/pattern?

我有一个 StringBuffer,它包含以前来自一个文件的多行内容,所有内容都基于较大行的模式合并在一起以进行比较。进行更改后,我基本上需要撤消将行合并在一起以重新写入新的 StringBuffer 以按原样重新写入文件。

因此,例如,文件包含这样一行(全部在一行中):

'macro NDD89 Start;Load; shortDescription; macro(continued) stepone;steptwo,do stuff macro(continued) stepthree;more steps; do stuff macro(continued) finalstep'

我需要将该行重写为多行(在有字符串 "macro(continued)" 的地方拆分):

macro NDD89 Start;Load; shortDescription;
macro(continued) stepone;steptwo,do stuff
macro(continued) stepthree;more steps; do stuff
macro(continued) finalstep'

如果有帮助,这是我的代码片段,它执行第一部分的操作,将原始文件放在多行位置并将它们组合成组并写入新文件:

    StringBuffer sb = new StringBuffer();
    Buffer = new FileReader(C:/original.txt);
    BufferedReader User_File = new BufferedReader(Buffer);
    String Line_of_Text;
    while((Line_of_Text = User_File.readLine()) != null){
        if(Line_of_Text.startsWith("macro ")){
            sb.append("\n"+Line_of_Text);
        }else if(Line_of_Text.startsWith("macro(continued)")){
            sb.append(Line_of_Text);
        }else{
            sb.append(Line_of_Text+"\n");
        }
    }
    BufferedWriter OutFile = new BufferedWriter(new FileWriter("C:/newFile.txt"));
    OutFile.write(sb.toString());
    OutFile.flush();
    OutFile.close();

有一段时间我一直在想办法做到这一点,suggestions/ideas?

希望 String class 中的 split() 和 contains() 方法可以帮助到您。

参考:How to split a string in Java

问题是您有一个没有行的大字符串。您可以通过输入 System.out.println(Line_of_Text); 来测试 if(Line_of_Text.startsWith("macro ")){ 行上方的内容。那你能看出哪里出了问题。

我会将您的附加代码更改为:

String Line_of_Text = null;
while ((Line_of_Text = User_File.readLine()) != null) {
    Line_of_Text = User_File.readLine();
    String[] splitted = Line_of_Text.split("macro ");
    for (String part : splitted) {
        sb.append(part+"\n");
    }
}

这利用 .split() 方法将您的字符串拆分为一个字符串数组。您可以使用该数组中的项目来追加它们。

编辑:我看到你有多个分隔符。 .split() 采用正则表达式分隔符,因此您可以尝试:Line_of_Text.split("macro|macro\(continued\)");

您可以在给定的令牌上使用 String#split 来拆分您的 String

例子

String test = "'macro NDD89 Start;Load; shortDescription; macro(continued) stepone;steptwo,do stuff macro(continued) stepthree;more steps; do stuff macro(continued) finalstep'";
//                       // splits into String array, by
//                       //     | followed by...
//                       //     |  | literal text
//                       //     |  |   | double-escape on parenthesis, as
//                       //     |  |   | this is a regex pattern
String[] splitted = test.split("(?=macro\(continued\))");
System.out.println("Raw:\r\n");
// prints the raw array (hard to read, as inline)
System.out.println(Arrays.toString(splitted)); 
// creates a list from the array and print each element one per line, trimmed
List<String> splittedList = Arrays.asList(splitted);
System.out.println("\r\nNice:\r\n");
for (String s: splittedList) {
    System.out.println(s.trim());
}

输出

Raw:

['macro NDD89 Start;Load; shortDescription; , macro(continued) stepone;steptwo,do stuff , macro(continued) stepthree;more steps; do stuff , macro(continued) finalstep']

Nice:

'macro NDD89 Start;Load; shortDescription;
macro(continued) stepone;steptwo,do stuff
macro(continued) stepthree;more steps; do stuff
macro(continued) finalstep'

最简单的解决方案!

String x =oneLine.replace(" macro", "\nmacro");

快速修复 QuakCore 的答案。

String breaker = "macro(continued)";
String x =oneLine.replace(" "+breaker, "\n"+breaker);