解析字符串java

时间:2017-05-22 01:57:27

标签: java parsing

我有以下字符串:

[19.47690987, -71.344714339999996]  6   2011-08-28 19:02:30 @Ninosh_Flow Jajajaj mi hermanasoooo
[35.106754240000001, -92.455415599999995]   6   2011-08-28 19:02:30 Your always on my mind
[40.973435600000002, -73.826951690000001]   6   2011-08-28 19:02:30 #nothingwrongwith having high standards...

我想解析它:

坐标:[19.47690987, -71.344714339999996]

日期:2011-08-28

时间:19:02:30

行:@Ninosh_Flow Jajajaj mi hermanasoooo

最好的方法是什么? 我有超过10万条这样的线路?

我尝试过使用.split

的不同变体
line.split(" 6 ")[0], line.split(" ")[3], line.split(" ")[4], line.split(" ")[5])

我想这不是我需要的东西

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

File myFile = new File("path-to-my-file");
Scanner scn = new Scanner(myFile);

while (scn.hasNextLine()) {
    scn.useDelimiter("\\]");
    String coord = scn.next() + "]";
    scn.useDelimiter("\\p{javaWhitespace}+");
    scn.next();  // Throw away ] delim
    scn.next();  // Throw away junk between coord and date
    String date = scn.next();
    String time = scn.next();
    String line = scn.nextLine();
    System.out.println("Coodinates: " + coord);
    System.out.println("Date: " + date);
    System.out.println("Time: " + time);
    System.out.println("Line: " + line);
}