Match Regex with Numeric Value and Decimal

Decimal or numeric values in regular expression validation

A digit in the range 1-9 followed by zero or more other digits:

^[1-9]\d*$

To allow numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:

^[1-9]\d*(\.\d+)?$

Notes:

  • The ^ and $ anchor to the start and end basically saying that the whole string must match the pattern

  • ()? matches 0 or 1 of the whole thing between the brackets

Update to handle commas:

In regular expressions . has a special meaning - match any single character. To match literally a . in a string you need to escape the . using \. This is the meaning of the \. in the regexp above. So if you want to use comma instead the pattern is simply:

^[1-9]\d*(,\d+)?$

Further update to handle commas and full stops

If you want to allow a . between groups of digits and a , between the integral and the fractional parts then try:

^[1-9]\d{0,2}(\.\d{3})*(,\d+)?$

i.e. this is a digit in the range 1-9 followed by up to 2 other digits then zero or more groups of a full stop followed by 3 digits then optionally your comma and digits as before.

If you want to allow a . anywhere between the digits then try:

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

i.e. a digit 1-9 followed by zero or more digits or full stops optionally followed by a comma and one or more digits.

Match regex with numeric value and decimal

You need to handle the two possibilities (numbers without a decimal part and numbers without an integer part):

/\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/
#^ ^ ^ ^^ ^---- end of the string
#| | | |'---- without integer part
#| | | '---- OR
#| | '---- with an optional decimal part
#| '---- non-capturing group
#'---- start of the string

or make all optional and ensure there's at least one digit:

/\A-?+(?=.??\d)\d*\.?\d*\z/
# ^ ^ ^ ^---- optional dot
# | | '---- optional char (non-greedy)
# | '---- lookahead assertion: means "this position is followed by"
# '---- optional "-" (possessive)

Note: I used the non-greedy quantifier ?? only because I believe that numbers with integer part are more frequent, but it can be a false assumption. In this case change it to a greedy quantifier ?. (whatever the kind of quantifier you use for the "unknow char" it doesn't matter, this will not change the result.)

Regex matching numbers and decimals

This is a fairly common task. The simplest way I know of to deal with it is this:

^[+-]?(\d*\.)?\d+$

There are also other complications, such as whether you want to allow leading zeroes or commas or things like that. This can be as complicated as you want it to be. For example, if you want to allow the 1,234,567.89 format, you can go with this:

^[+-]?(\d*|\d{1,3}(,\d{3})*)(\.\d+)?\b$

That \b there is a word break, but I'm using it as a sneaky way to require at least one numeral at the end of the string. This way, an empty string or a single + won't match.

However, be advised that regexes are not the ideal way to parse numeric strings. All modern programming languages I know of have fast, simple, built-in methods for doing that.

Regular expression to match number with Decimal separator and optional Thousands separator

The reason the second alternative isn't matching is because it only allows a single \f after the decimal point. That needs to be \d+.

Then you need to wrap everything between ^ and $ in a group, so all alternatives match the entire string.

You had lots of redundant parentheses. And \d* in the last alternative should be \d+, otherwise you'll allow a number that's completely empty or just a sign.

^[+-]?([0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?|\d*\.\d+|\d+)$
  • ^ -> start of string
  • [+-]? -> matches optional + or - char
  • ([0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?|\d*\.\d+|\d+) -> whole group
    has to match [0-9]{1,3}(,[0-9]{3})*(\.[0-9]+) or \d*\.\d+ or \d+
    • [0-9]{1,3}(,[0-9]{3})*(\.[0-9]+) -> matches numbers with thousand separators and maybe decimal separator
    • \d*\.\d+ -> matches numbers with decimal separator, and maybe digits before the decimal
    • \d+ -> matches numbers without decimal separator
  • $ -> end of string

DEMO

Regex matching numbers greater than a decimal value

i'm not sure if this fully address your rquirements I wasn't able to comment but I think you can filter zero matchees by a lookahead: (?!0+\.0+$)^\d+(?:\.\d{1,2})?$

Using a regex to match only decimal numbers but I keep matching non-single-digit numbers

Your regex:

check_dec=^[0-9]*\.+[0-9]+

Has 2 main problems:

  1. No quoting thus effectively making it "=^[0-9]*.+[0-9]+". Note that in regex dot matches any character hence this (incorrect) regex means:
  • Match 0 or more digits i.e. [0-9]*
  • Match 1+ of any character i.e. .+
  • Match 1+ of digits [0-9]+

So clearly this regex will require at least 2 characters in input and it should end with digits


  1. As evident from previous comment that other than quoting issue regex itself is not correct.

Correct regex to match decimal numbers like 123.45 would be:

check_dec='^[0-9]*\.[0-9]+'

Note no quantifier + after dot allowing only one dot in number and quotes around.

RegEx needed to match number to exactly two decimal places

^[0-9]*\.[0-9]{2}$ or ^[0-9]*\.[0-9][0-9]$

Regex match numbers from 1-10, including decimals

If you want to go down the regex route then I believe this horrible regex matches your requirements:

(?<![\d\.-])\d(\.\d)?(?!(\.\d)|\d)|(?<![\d\.-])10(?!(\.\d)|\d])

How it works:

If you remove the boundary conditions then the regex looks much simpler:

\d(\.\d)? | 10

This matches EITHER "a digit, optionally followed by a period AND a digit" OR "the number 10" (with no decimal places) as per your requirements.

The other parts of the regex add lookaround behaviour to ensure that the regex doesn't match negative numbers, or digits followed by a period and more digits. This is to ensure that something like this:

10.5

doesn't yield two different matches: "10" and then "5".

Note

This will NOT match .5 .7 .9 (but will match 0.5 0.7 0.9)

Also, it will NOT match 10.0 (but will match 10)

View Online Example HERE

How to put regex expression to validate number between 1.0 to 4.5 (range between decimal values)?

This is a regex that I created.
pattern(/^[1-3]{0,1}(?:[.][0-9]{0,1})?[4]{0,1}(?:[.][0-5]{0,1})?$/)

Explaination

/^[1-3]{0,1}(?:[.][0-9]{0,1})?[4]{0,1}(?:[.][0-5]{0,1})?$/

/^ start of the input
[1-3] First input to be taken between
{0,1} This shows how many times it can be repeated
( Parenthesis shows next entered digit is optional
? Question mark is for using digit zero or once
:[.] This is for what the next Character would be eg ".", "@"
[0-9] input to be taken between.
{0,1} This shows how many times it can be repeated.
) Option part closes
? Question mark is for using digit zero or once.
[4] This shows what can be other input that can be taken
{0,1} How many times it can be use , SO in this case 0 or 1 times

(?:[.] There after again same option part with decimal point

decimal value of 4and its limitation for 0 to 5
[0-5] This is to set limitation 0-5 as per our requirement
{0,1} Its existence 0 or 1 times
) Close of optional part.
? Thereafter showing the existence of an optional part once or twice.
$/ Shows to match expression after it.



Related Topics



Leave a reply



Submit