Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java delimiter while reading text file - regex/or not?

I am trying to read a text file written in this form:

    AB523:[joe, pierre][charlie][dogs,cat]
    ZZ883:[ronald, zigomarre][pele]

I would like to create my structure and retrieve the information properly.

AB523 --- alone
joe,pierre ---alone
charlie ---alone
dogs,cat --- alone

I am not sure what's the best technique that should be used. I've tried StringTokenizer ...and played with regEx but I can't get it right

Do you have any solution? or suggestion

What's is the convention when writting in a text file? What are the best pratices with delimiters?

EDIT:The textfile is also generated by me, so I have control over the overall pattern. What would be the best output pattern to reduce the amount of work when re-reading it ?

like image 214
user1023021 Avatar asked May 31 '26 01:05

user1023021


2 Answers

I would use regular expressiones here, because it seems like less code to maintain, and your language is certainly regular. Along with a java.util.Scanner instance for more efficiency. Here's some code:

import java.io.Reader;
import java.io.StringReader;
import java.util.Scanner;
import java.util.regex.Pattern;

public class ScannerTest {

private static final Pattern header = Pattern.compile("(.*):");
private static final Pattern names = Pattern.compile("\\[([^\\]]+)\\]");

public static void main(String[] args) {

    Reader reader = new StringReader(
            "AB523:[joe, pierre][charlie][dogs,cat]\n"
                    + "ZZ883:[ronald, zigomarre][pele]");

    Scanner scanner = new Scanner(reader);
    scanner.useDelimiter("\n");

    while (scanner.hasNext()) {
        String h = scanner.findInLine(header);
        // Substring removes trailing ':'.
        System.out.println(h.substring(0, h.length() - 1));

        String n;
        while ((n = scanner.findInLine(names)) != null)
            // Substring removes '[' and ']'.
            System.out.println(n.substring(1, n.length() - 1));

        if (scanner.hasNext())
            scanner.nextLine();
    }
}
}

Nevertheless, I still couldn't manage to remove the substring invocations, and maybe that hides some inefficiency. My guess is that not, due to the immutability of strings, strings shouldn't be recreated for this case.

EDIT: for better performance I would also consider a handcrafted recursive descent parser.

like image 168
Martín Schonaker Avatar answered Jun 01 '26 15:06

Martín Schonaker


Use String#split or Pattern#split Method. For example,

   String[] list ="AB523:[joe, pierre][charlie][dogs,cat]".split("[:\\[\\]]+");
   for(String s : list)
       System.out.println(s);
like image 26
Prince John Wesley Avatar answered Jun 01 '26 16:06

Prince John Wesley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!