简体   繁体   English

Java正则表达式逗号分隔数字

[英]java regular expression comma separated numbers

I am trying to get 1 long (timestamp) and 3 floats out of the payload of a message, the string I am passing looks like this (ignore quotes): 我试图使1长(时间戳)和3浮出一条消息的有效负载,我传递的字符串看起来像这样(忽略引号):

"5737540811294,359.306580,7.948747,6.6707006621"

with this 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 the debug code with: 我通过以下方式不断访问调试代码:

DEBUG processAnglesMsg: 5737540811294,359.306580,7.948747,6.6707006621* 调试过程角度消息:5737540811294,359.306580,7.948747,6.6707006621 *

so it looks like the pattern I have is not working and data.matches() is returning false, despite much searching I can't see what I have done wrong. 所以看起来我的模式无法正常工作,尽管进行了大量搜索,但我看不到自己做错了什么,data.matches()返回的是false。 I do want the pattern to allow for optional + or - even though my current data doesn't contain this. 我确实希望模式允许可选的+或-,即使我当前的数据中不包含此选项。

The problem with your sub- Pattern s is that the + / - sign is not optional and will not match in your example (also the last sub-pattern requires a comma, but your example doesn't end with one). 您的子Pattern的问题是+ / -号不是可选的,并且在您的示例中不匹配(同样,最后一个子模式需要逗号,但是您的示例不以逗号结尾)。

Assuming your example String s will never contain a comma for localized number separating purposes, and that you do not want to use a CSV parsing framework: 假设示例String绝不会包含用于本地化数字分隔目的的逗号,并且您不想使用CSV解析框架:

String example = "5737540811294,359.306580,7.948747,6.6707006621";
// splits the input based on comma
String[] split = example.split(",");
// parses desired data types
// will 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 also use BigDecimal instead of Float if you wish to keep as many decimals as possible. 如果希望保留尽可能多的小数,也可以使用BigDecimal而不是Float

For instance: 例如:

System.out.println(new BigDecimal(split[3]));

Output 产量

6.6707006621

Use a scanner: 使用扫描仪:

Scanner scanner = new Scanner(input).useDelimiter(",");
long time = scanner.nextLong();
float yaw = scanner.nextFloat();
float pitch = scanner.nextFloat();
float roll = scanner.nextFloat();

Easier to code and easier to read. 易于编码和阅读。


If the input will not always match: 如果输入不总是匹配:

Scanner scanner = new Scanner(input).useDelimiter(",");
try {
    long time = scanner.nextLong();
    float yaw = scanner.nextFloat();
    float pitch = scanner.nextFloat();
    float roll = scanner.nextFloat();
    System.out.format("Angles - [%8d] Yaw: %08.3f Pitch: %08.3f Roll: %08.3f%n",time,yaw, pitch,roll);
} catch (NoSuchElementException e) {
    // input didn't match due to insufficient elements or parsing failure
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM