Search code examples
regexfloating-pointnumbers

Regular expression to match two numbers


I need a regular expression that matches 2 float numbers that could be separated by a space, comma or nothing.

A float number could be integer (made of digits), a fraction (digits, decimal point, digits), or remainder skipping leading zero (decimal dot, digits). It can also be preceded by optional plus or minus sign. Float numbers do not contain spaces.

Examples of valid floating point numbers: 345678, 976.4567, .765456, +8765456, -876.345, -.8765345

Now I want to match two numbers that could be separated by any number of spaces, a comma (with optional spaces), or nothing .i.e. concatenated together, but only in the case the second float does not start with a digit.

Two numbers separated by spaces: 8765 2345.34567

Two numbers separated by a comma: 3456.8765,567.234

Two numbers separated by a comma with spaces: 567865.667 , +87567.234323

Examples of two numbers concatenated, but should still match as separate:

5673+8667.45 -> 5673, +8667.45

-0977.345.3456 -> -0977.345, .3456

780-875 -> 780, -875

+098+.45 -> +098, +.45

To make things harder, the float subexpressions must be grouped because I want to parse them. So the negative or positive sign must be included in the group.

My current solution errorneously matches a single number, treating the last digit as the second number.

85678.96783 -> errorneusly matches as: 85678.9678, 3

Such a string must not be matched by the expression.


Solution

  • You can use (?>...) for an atomic (non-backtracking) non-capturing group, e.g.

    ([+-]?(?>[0-9]+(?:[.][0-9]*)?|[.][0-9]+))\s*,?\s*([+-]?(?>[0-9]+(?:[.][0-9]*)?|[.][0-9]+))
    

    The floating point matcher [+-]?([0-9]+([.][0-9]*)?|[.][0-9]+) is taken from here: https://stackoverflow.com/a/12643073. I made the inner groups non-capturing (?:...) and the digit-parts atomic.

    Test-code: https://regex101.com/r/ZWNLBU/1 (derived from @Thefourthbird's link)