Last Updated
Viewed 18 Times
           

I'm trying to validate an amount formatted with decimals and commas and it should allow only numbers.

True values:

1.000.000,00
2.000,23
4.000.000.000

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):

"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*

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. I do want the pattern to allow for optional + or - even though my current data doesn't contain this.

I have this regular expression:

^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$

however it is failing when i have an amount such as this: 41022095.6

anything I am missing?

Looking for the above

1,2,3 is allowed 1, ____2_____,3_____, is allowed where _ is space 1 _ 2 _ 3 is not allowed where _ is space

Thanks

Similar Question 7 (1 solutions) : Regular expression filter angular2

Similar Question 8 (5 solutions) : Regular expression with comma and numbers

Similar Question 9 (1 solutions) : Regular expression for comma separated string

cc