TransWikia.com

How to implement negative null floating point regex

Stack Overflow Asked by frontDev24 on December 14, 2020

What could I use as a regex that validates floating point numbers?

The numbers could be negative and cannot start by 0 (unless it is a real 0, or a 0 followed by decimal points such in 0.00123)

On the other hand, the regex should validate partial/uncompleted numbers. For example:

  • ”(empty string) is valid
  • - is valid (it could still be a valid number with appropiate user input)
  • 0 is valid (as 0.1 could be valid)
  • 0.0 is valid, for the same previous reason
  • -0.000 is valid
  • -01 is not valid
  • .2 is not valid

I tried with this regexp:

 ^(-|-?0.?[0-9]*|-?[1-9][0-9]*.?[0-9]*|)$

It allows me to discard all non-numeric values ​​and still allow fractional values, but how to implement the possibility that the user cannot enter numbers in the format -0232, but can enter -0.232

2 Answers

If I get it right, you want a regex that validates "could-be-a-future-decimal" numbers. So (empty string) -, -0, 0 and 0.... should be valid.

You may use this regex:

^-?(?:(?!0d)d+.?d*)?$

You have a demo here.

Explained:

^             # Start of string
-?            # an optional minus
(?:           # non-capturing group
    (?!0d)   #    whatever comes next, it cannot be 0 + number
    d+       #    at least one number
    .?       #    optional dot
    d*       #    any quantity of numbers
)?            # previous group is optional
$

Also, bear in mind that the previous thing is a regex object. So you should try to use it like this in javascript:

/myregex/

However, javascript also allows using string-based regexes (which by your comments it seems that is what you want...)

In that case you can use the new RegExp syntax. However, bear in mind that since you'll be using a string, you need to scape any characters a string should scape.

For example, you may use:

function return_some_regex_string() {
  // NOTE backslashes had to be scaped since this is a string, 
  //      not a real regex object
  return '^-?(?:(?!0\d)\d+\.?\d*)?$';
}

console.log("0.123".match(new RegExp(return_some_regex_string())));
console.log("-03233".match(new RegExp(return_some_regex_string())));
console.log("333-3334".match(new RegExp(return_some_regex_string())));

But, at the same time, you could already return the regex object, which seems a simpler way to work than using string-based regexes:

function return_some_regex() {
  return /^-?(?:(?!0d)d+.?d*)?$/;
}

console.log("0.123".match(return_some_regex()));
console.log("-03233".match(return_some_regex()));
console.log("333-3334".match(return_some_regex()));

Correct answer by Julio on December 14, 2020

You could match an optional hyphen, followed by either a floating point or match a number starting with 1-9

^-?(?:d+.d+|[1-9]d*|0.?)$

The pattern will match

  • ^ Start of string
  • -? Optional hyphen
  • (?: Non capture group
    • d+.d+ Match 1+ digits, dot and 1+ digits
    • | Or
    • [1-9]d* Match a digit 1-9 and 0+ digits 0-9
    • | Or
    • 0.? Match a zero and optional dot
  • ) Close group
  • $ End of string

Regex demo

let pattern = /^-?(?:d+.d+|[1-9]d*|0.?)$/;
["-0123",
  "0123123",
  "-03123123",
  "1234",
  "-0.234",
  "0.4",
  "0.3312312",
  "1",
  "-1234",
  "0",
  "0."
].forEach(s => console.log(`${s} match:${pattern.test(s)}`));

If you want to match an empty string or just a single hyphen, you can also make the second part optional.

^-?(?:d+.d+|[1-9]d*|0.?)?$

Regex demo

Answered by The fourth bird on December 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP