Search code examples
javaregexscala

Regex for matching different float formats


I'm looking for a regex in scala to match several floats:

    9,487,346 -> should match
    9.487.356,453->should match
    38,4 -> match
    -38,4 -> should match
    -38.5
    -9,487,346.76
    -38 -> should match
     

So basically it should match a number that:

  1. possibly gave thousand separators (either comma or dot)
  2. possibly are decimal again with either comma or dot as separator

Currently I'm stuck with

    val pattern="\\d+((\\.\\d{3}+)?(,\\d{1,2}+)?|(,\\d{3}+)?(\\.\\d{1,2}+)?)" 

Edit: I'm mostly concered with European Notation.

Example where the current pattern not matches: 1,052,161

I guess it would be close enough to match that the String only contains numbers,sign, comma and dot


Solution

  • If, as your edit suggests, you are willing to accept a string that simply "contains numbers, sign, comma and dot" then the task is trivial.

    [+-]?\d[\d.,]*
    

    update

    After thinking it over, and considering some options, I realize that your original request is possible if you'll allow for 2 different RE patterns, one for US-style numbers (commas before dot) and one for Euro-style numbers (dots before comma).

    def isValidNum(num: String): Boolean =
      num.matches("[+-]?\\d{1,3}(,\\d{3})*(\\.\\d+)?") ||
        num.matches("[+-]?\\d{1,3}(\\.\\d{3})*(,\\d+)?")
    

    Note that the thousand separators are not optional, so a number like "1234" is not evaluated as valid. That can be changed by adding more RE patterns: || num.matches("[+-]?\\d+")