简体   繁体   中英

Java Scanner to split records by an empty line

I have records formatted the following way:

record 1 line
record 1 line
record 1 line

record 2 line
record 2 line
record 2 line

...

I'd like to use a java Scanner to pull each record's lines out. However, my patter does not seem to be working correctly.

new Scanner(reader).useDelimiter(Pattern.compile("^\\s*$"));

I get back my input rather then scanner.next() giving me the lines for each record.

The solution I came up with was this:
new Scanner(reader).useDelimiter(Pattern.compile("^\\\\s*$", Pattern.MULTILINE));

This will return the record lines as a group:
scanner.next()
yields:
record 1 line 1
record 1 line 2
record 1 line 3

doing it again scanner.next()
yields:
record 2 line 1
record 2 line 2
record 2 line 3

If you desire just the lines individually, use @bart 's solution.

我想要多行尝试

"\\n\\n+"

This is a bit of guess, but do you really need to use regex here? I think you just need to set the delimiter as a new line char.

I would do it like this.

StringBuilder sb = new StringBuilder();
ArrayList<String> records = new ArrayList<String>();
while(scanner.hasNextLine()){
    String line = scanner.nextLine();
    if("".equals(line.trim())){
        records.add(sb.toString());
        sb = new StringBuilder();
    }else{
        sb.append(line);
        sb.append("\n");
    }
}
System.out.println(records);

I didn't test this so it might have some silly errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM