Is there a regular ^([0-9]*\.[0-9]+)$ , how to make it skip floating-point and comma numbers?

    3 answers 3

    Replace \. on [.,] and remove the extra brackets:

     ^[0-9]*[.,][0-9]+$ 

    To skip integers, add a quantifier ? :

     ^[0-9]*[.,]?[0-9]+$ ^ 

    See the demo

    • ^ - beginning of line
    • [0-9]* - 0 or more digits
    • [.,] - point or comma ( [,.]? - one or zero commas or points)
    • [0-9]+ - 1 or more digits
    • $ - end of line.

    If you need a more "advanced" expression, you can use

     ^[-+]?[0-9]*[.,][0-9]+(?:[eE][-+]?[0-9]+)?$ 

    or - to support both integers and fractional numbers:

     ^[-+]?[0-9]*[.,]?[0-9]+(?:[eE][-+]?[0-9]+)?$ 

    This is a variant expression on regular-expressions.info .

      There are several entries for floating point numbers. If you mean the presence of a single point or a comma, then you can:

       ^\d+(?:[\.,]\d+)?$ 

      [0-9] * - your case will skip an invalid construction, for example .5656

      • why is it invalid? Most languages ​​understand it. - pavel
      • I'm talking about a specific case. [0-9] * means 0 and more, that is, 345345 will miss [0-9] + means 1 and more - mkardakov

      So:

       ^(0|[1-9]\d*)([.,]\d+)? 

      resolves integers, fractional ones, excludes the variant with several zeros before the fractional part, on the similarity: "000.0001".

      New member
      Nikolay Muzyka is a new member of the site. Be lenient in asking clarifying questions, commenting and answering. Read about the norms of behavior .