java - Best way to split strings and get variables from it -


let's need read log file. once log file reach line there status 'i important log' in need stuff line.

string exampleofwhatstrlinereturns ="2016-02-06 10:19:36,410 info  [[requestmappinghandlermapping]]: important log [123, 21] ...{.....}";  string id = "123"; string anothervalueiwant = "21"; string timestampstring = "2016-02-06 10:19:36,410"; 

what best way achieve this?

you can follows:

  1. extracting timestamp

search first occurrence of info , take sub-string 0 position - 1.

  1. extracting id & anothervalueiwant

search regex patter [0-9, ] , split contents , , take 0 id , 1 anothervalueiwant.

here quick code snippet:

public static void main (string[] args) {     string sample = "2016-02-06 10:19:36,410 info  [[requestmappinghandlermapping]]: important log [123, 21] ...{.....}";      int position = sample.indexof("info");     system.out.println("timestamp: " + sample.substring(0,position-1));      pattern my_pattern = pattern.compile("\\[[0-9, ]+\\]");     matcher m = my_pattern.matcher(sample);     while (m.find()) {         string str = m.group(0);         string[] s = str.substring(1,str.length()-2).split(", ");         system.out.println("id: " + s[0]);         system.out.println("anothervalueiwant: " + s[1]);     } } 

output:

timestamp: 2016-02-06 10:19:36,410 id: 123 anothervalueiwant: 2 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -