regex - java regular expression comma separated numbers -


i trying 1 long (timestamp) , 3 floats out of payload of message, string passing looks (ignore quotes):

"5737540811294,359.306580,7.948747,6.6707006621" 

with method

private static void processanglesmsg(string s) {    final pattern datatfffsplit = pattern.compile( "[+-]([0-9]+),"                                                 + "[+-]([0-9]*[.])?[0-9]+,"                                                 + "[+-]([0-9]*[.])?[0-9]+,"                                                 + "[+-]([0-9]*[.])?[0-9]+,");     matcher data = datatfffsplit.matcher(s.trim());     if(data.matches()) {         long time = long.parselong(data.group(1));         float yaw = float.parsefloat(data.group(2));         float pitch = float.parsefloat(data.group(3));         float roll = float.parsefloat(data.group(4));         system.out.format("angles - [%8d] yaw: %08.3f pitch: %08.3f roll: %08.3f%n",time,yaw, pitch,roll);     } else {         if(debuglevel>=4) system.out.println("debug processanglesmsg: "+s);     } } 

i keep reaching debug code with:

debug processanglesmsg: 5737540811294,359.306580,7.948747,6.6707006621*

so looks pattern have not working , data.matches() returning false, despite searching can't see have done wrong. want pattern allow optional + or - though current data doesn't contain this.

the problem sub-patterns + / - sign not optional , not match in example (also last sub-pattern requires comma, example doesn't end one).

assuming example strings never contain comma localized number separating purposes, , not want use csv parsing framework:

string example = "5737540811294,359.306580,7.948747,6.6707006621"; // splits input based on comma string[] split = example.split(","); // parses desired data types // throw runtime numberformatexception if something's un-parseable system.out.println(long.parselong(split[0])); system.out.println(float.parsefloat(split[1])); system.out.println(float.parsefloat(split[2])); system.out.println(float.parsefloat(split[3])); 

output (values rounded)

5737540811294 359.30658 7.948747 6.6707006 

note

you can use bigdecimal instead of float if wish keep many decimals possible.

for instance:

system.out.println(new bigdecimal(split[3])); 

output

6.6707006621 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -