【发布时间】:2020-06-18 19:50:22
【问题描述】:
下面是一个sn-p的文本文件格式结构
Historical Sales for: 12th of October 2019, 11:37 am
PRODUCT NAME QUANTITY
Coke B 5
Historical Sales for: 21st of October 2019, 8:15 pm
PRODUCT NAME QUANTITY
Peanuts 2
我只想处理列标签和行值,但不包括主标题;在本例中,历史销售额为:2019 年 10 月 12 日上午 11:37。
这是我编写的使用正则表达式(\\b)处理文本的代码
StringBuilder temporary = new StringBuilder();
InputStream inputStream = new FileInputStream(new File(FILE_NAME));
BufferedReader readFile = new BufferedReader(new InputStreamReader(inputStream));
String next;
while ((next = readFile.readLine()) != null) {
temporary.append(next).append("\n");
}
next = String.format("%13s", ""); // spacing for column headers
System.out.println(temporary.toString().replaceAll("(\\b)", next));
【问题讨论】:
-
\\b{3}在每个单词边界位置匹配一个空字符串 3 次。因此,有效地匹配一个空字符串。\\b{3}=\\b -
您应该为示例文本文件使用预先格式化的文本块(代码块),而不是文本文件的图片。 —— 这是一个包含多个“Historical Sales for:”标题的单个文本文件,还是每个都有一个标题的多个文件?标题的一致性如何?如果标题本身符合模式,也许您可以在处理文件时匹配并丢弃标题。
-
for (; <condition> ;)与while (<condition>)相同——while是一个更自然的构造......while ( line = readFile.readLine() ) != null) { if (isHeaderLine(line) { continue; } temporaryData.append..... } -
@Edward 它匹配文件中的整个文本,这不是我想要的。我的目标是完全丢弃标题 Historical Sales for: 和相应的日期与时间
-
正则表达式
\\b匹配a \ + b。演示->regex101.com/r/XIvBBb/1。同样,据我所知,没有\\b in the Java language。
标签: java regex replace file-processing