当不同行在不同位置有空格时如何根据空格分割记录

我有一个包含如下记录的文件,我试图根据空格分割其中的记录并将它们转换为逗号。


文件:


a 3w 12 98 header P6124

e 4t 2  100 header I803

c 12L 11 437       M12



BufferedReader reader = new BufferedReader(new FileReader("/myfile.txt"));

String line = reader.readLine();

while (line != null) {

System.out.println(line);

line = reader.readLine();

String[] splitLine = line.split("\\s+")

如果数据由多个空格分隔,我通常会使用正则表达式替换 ->split('\\s+')或split(" +")。但在上述情况下,我有一条c没有数据的记录header。因此,正则表达式“\s+”或“+”将跳过该记录,我将得到一个空白空间,而c,12L,11,437,M12不是c,12L,11,437,,M12


在这种情况下,如何根据任何分隔符正确分割行,以便获得以下格式的数据:


a,3w,12,98,header,P6124

e,4t,2,100,header,I803

c,12L,11,437,,M12

谁能让我知道如何实现这一目标?


炎炎设计
浏览 109回答 3
3回答

慕哥9229398

也许您可以尝试使用更复杂的方法,使用复杂的正则表达式来匹配每行的六个字段,并显式处理第五个字段缺失值的情况。我重写了您的示例,添加了一些控制台日志,以澄清我的建议:public class RegexTest {&nbsp; &nbsp; private static final String Input = "a 3w 12 98 header P6124\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "e 4t 2&nbsp; 100 header I803\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "c 12L 11 437&nbsp; &nbsp; &nbsp; &nbsp;M12";&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader reader = new BufferedReader(new StringReader(Input));&nbsp; &nbsp; &nbsp; &nbsp; String line = null;&nbsp; &nbsp; &nbsp; &nbsp; Pattern pattern = Pattern.compile("^([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)? +([^ ]+)$");&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line = reader.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(line != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] splitLine = line.split("\\s+");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(splitLine.length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Line: " + line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Matcher matcher = pattern.matcher(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("matches: " + matcher.matches());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("groups: " + matcher.groupCount());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 1; i <= matcher.groupCount(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("&nbsp; &nbsp;Group %d has value '%s'\n", i, matcher.group(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } while (line != null);&nbsp; &nbsp; }}关键是用于匹配每一行的模式需要六个字段的序列:对于每个字段,值描述为[^ ]+字段之间的分隔符描述为+第五个(可为空)字段的值描述为[^ ]+?使用括号将每个值捕获为一个组:( ... )每行的开始 (&nbsp;^) 和结束 ( ) 都被明确标记$然后,每一行都与给定的模式进行匹配,获得六个组:您可以使用 访问每个组matcher.group(index),其中index是从1 开始的,因为group(0)返回完全匹配。这是一种更复杂的方法,但我认为它可以帮助您解决问题。

富国沪深

对可用于分割输入的空白字符数量进行限制。对于您的示例数据,最多 5 个有效:String[] splitLine = line.split("\\s{1,5}");

慕盖茨4494581

您只是想将分隔符从空格切换为逗号吗?在这种情况下:&nbsp;cat myFile.txt | sed 's/ &nbsp; */ &nbsp;/g' | sed 's/ /,/g'*编辑:添加了一个阶段来删除超过两个空格的列表,将它们替换为保留双逗号所需的两个空格。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java