gpt4 book ai didi

java - 从文本文件中的一行中删除单词

转载 作者:行者123 更新时间:2023-11-30 07:00:22 27 4
gpt4 key购买 nike

我正在尝试删除时间表行中的单词 alle 30 Min。目前正在删除 30Min,但不删除单词 alle。我该如何解决?

感谢任何帮助。

示例

apple 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
chairalle 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
tablealle 05:09 05:39 06:11 06:41 07:11 07:41 08:11 alle 17:11 17:41 18:11 18:41 19:11 19:41
spoonalle 05:11 05:41 06:14 06:44 07:14 30 07:44 08:14 17:14 17:44 18:14 18:44 19:14 19:44
cabelalle 05:13 05:43 06:17 06:47 07:17 07:47 08:17 17:17 17:47 Min 18:17 18:47 19:17 19:47

Excepted output without alle 30 Min:

apple 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
chairalle 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
tablealle 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
spoonalle 05:11 05:41 06:14 06:44 07:14 07:44 08:14 17:14 17:44 18:14 18:44 19:14 19:44
cabelalle 05:13 05:43 06:17 06:47 07:17 07:47 08:17 17:17 17:47 18:17 18:47 19:17 19:47

代码:

    Scanner scannerLines = new Scanner(file)) {

while (scannerLines.hasNextLine()) {
String line = scannerLines.nextLine();
if (line.contains(" alle ")) {
//Delete alle from the line.
line.replace(" alle ", " ");
String nextLine = scannerLines.nextLine();
Pattern pattern = Pattern.compile("\\s\\d\\d\\s");

Matcher m = pattern.matcher(nextLine);
while (m.find()) {
value = Integer.parseInt(m.group().trim());
line.replace(m.group(), " ");

String nextLine2 = scannerLines.nextLine();
nextLine2.replace("Min", " ");
System.out.println(value);
}
}
writer.println(line);
}

最佳答案

结果字符串赋值回行!

line = line.replace(" alle ", " ");

引用String#replace


工作示例:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class TestReplace {

public static void main (String [] args) {

File inputFile = new File("Input.txt");
File outputFile = new File("Output.txt");

Scanner sc = null;
FileWriter fw = null;

try {
sc = new Scanner(inputFile);
fw = new FileWriter(outputFile);

while(sc.hasNextLine()) {
String str = sc.nextLine();
str = str.replaceAll("\\s30\\s|\\salle\\s|\\sMin\\s", " ");
fw.write(str + "\r\n");
}

sc.close();
fw.close();

} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

输出:

apple 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
chairalle 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
tablealle 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
spoonalle 05:11 05:41 06:14 06:44 07:14 07:44 08:14 17:14 17:44 18:14 18:44 19:14 19:44
cabelalle 05:13 05:43 06:17 06:47 07:17 07:47 08:17 17:17 17:47 18:17 18:47 19:17 19:47

关于java - 从文本文件中的一行中删除单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30680319/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com