Manu Manu - 5 months ago 11
Java Question

Reading a set of values from a text file

{
"TEST":"189456",
"TEST1":"X_Y_Z",
"TEST2":"Y_Z_W",
"TEST3":"GGG ",
"TEST4":"32423423233322"
},
{
"TEST":"123456",
"TEST1":"X_E_Z",
"TEST2":"T_Z_W",
"TEST3":"EWE ",
"TEST4":"324234243234"
}


This is a
.txt
file I want to read and print only 189456,123456 from the above file.Can anyone help me in doing this.Please find the code for reference.Please post the easiest code.....

Pattern p = Pattern.compile("\"Test\"\\s*:\\s*\"(.*)\"", Pattern.CASE_INSENSITIVE);

while ( (line = bf.readLine()) != null) {
linecount++;

Matcher m = p.matcher(line);

// indicate all matches on the line
while (m.find()) {

System.out.println(m.group(1));
}
}

Answer

As for a Regex solution :

(.*)\"TEST":\"(.*?)\"

Note the ? , it makes your regex to stop at the first match of ".

With spaces in between :

(.*)\"TEST"\s*:\s*\"(.*?)\"