Regular expression for a number with fractional and decimal part - c++

I am new to regular expression , i want a regular expression which satisfies following reqs:
User can enter a value in which the fractional part should not be more than 3 digits ,it can be less than 3 digits and the decimal part should not more than 1 digit.The number can be without decimal part in that case the digits should not more than 3.It is also possible to just have decimal part.Please help

Please try this,
^\d{0,3}(\.(?=\d)\d){0,1}$
This matches the followings,
123
1.2
12.3
123.4
I hope this helps.

Related

Regex for price, requiring decimal point and 2 decimal places

I am trying to validate a price field in Javascript.
The value can only be numbers, must have 1 decimal point, and must have 2 decimal places after it. Only 7 digits can be in front of the decimal point. Like: 1000000.00
Accepted:
123.00
1.01
0.01
4576.23
1234567.00
1.00
Not accepted:
0.00 (Cannot be free)
0.1 (not 2 decimal places)
1.0 (not 2 decimal places)
01.01 (Cannot start with 0)
12345678.00 (too many digits)
123 (no decimal point and 2 places)
-123.12 (negative, and unacceptable character)
123.123 (too many places)
I am unsure how to approach this problem and any help would be appreciated. A simple guide on how to do write my own regex would be helpful too as English is not my strong point. Thanks in advance.
Here's what I tried on my own: /^[0-9]+.[0-9]{2}$/
But I am unsure how to approach the 0 and length problem.
This regular expression will solve your problem. I have checked it against all the options you have given in question.
^(0(?!\.00)|[1-9]\d{0,6})\.\d{2}$
If you don't know how to test a regular expression against a string in JavaScript you can check below link.
http://www.w3schools.com/jsref/jsref_regexp_test.asp
Try this pattern ^(?!^0\.00$)(([1-9][\d]{0,6})|([0]))\.[\d]{2}$
It excludes 0.00 and negative numbers and any spaces before or after the number (negative cases)
Hope I covered all possibilities
You can check for test cases here
The following regex pattern matches lines with 1 up to 7 digits followed by a "." and 2 more digits excluding those starting with 0 (or a letter)
^[1-9]\d{0,6}\.\d{2}$

Regular expression for a decimal with a max of 2 digits before decimal point

I am trying to find a regular expression that works for a decimal with a max of 2 digits before the decimal point and 2 digits after the decimal point. The decimal point and decimal places are optional. So these values would be accepted :
90
5.4
45.21
0.5
0
And the would be rejected :
100
105.56
05.6
55.543
78.
Can any regex gurus help?
This should work:
^[1-9]\d?(?:\.\d{1,2})?$
If you want to accept even 0.5, try this:
^(?:[1-9]\d?|0)(?:\.\d{1,2})?$
I hope this will work for you
\d{0,2}(\.\d{1,2})?
/\A[1-9]?\d(?:\.\d{1,2})?\z/
..................

Regular expression for price validation

Need regular expression which have:
Maximum 8 digits before decimal(.) point
Maximum 4 digits after decimal point
Decimal point is optional
Maximum valid decimal is 8 digits before decimal and 4 digits after decimal
So 99999999.9999
The regular rexpression I have tried ^\d{0,8}[.]?\d{1,4}$ is failing for 123456789
and more than this. means it is taking more than 8 digits if decimal point is not available.
Tested here : http://regexpal.com/
Many many thanks in advance!
^\d{0,8}(\.\d{1,4})?$
You can make the entire decimal optional
You can try this:
^\d{1,8}(?:\.\d{1,4})?$
or
^[1-9]\d{0,7}(?:\.\d{1,4})?$
If you don't want to have a zero as first digit.
You can allow this if you want: (.1234)
^[1-9]\d{0,7}(?:\.\d{1,4})?|\.\d{1,4}$
Any of the above did not work for me.
Only this works for me
^([0-9]{0,2}((.)[0-9]{0,2}))$
This regex is working for most cases even negative prices,
(\-?\d+\.?\d{0,2})
Tested with the following,
9
9.97
37.97
132.97
-125.55
12.2
1000.00
10000.00
100000.00
1000000.00
401395011
If there is a price of $9.97, £9.97 or €9.97 it will validate 9.97 removing the symbol.
1-(\$+.[1-9])
2-(\£+.[1-9])
You can use this expression for complete price digits.
I'm using this:
^[1-9]\d{0,7}(\.\d{1-4})$
^ = the start of the string
[1-9] = at least the string has to begin with 1 number between 1 and 9
\d{0,7} = optional or max 7 times d (digit: a number between 0 and 9)
() = create a group like a substring
. = need a .
\d{1-4} = digit repited max 4 time
$ end of the string
For price validation we can not allow inputs with leading repeating zeros like 0012 etc.
My solution check for any cases. Also it allows maximum 2 decimal point after the dot.
^(?:0\.[0-9]{1,2}|[1-9]{1}[0-9]*(\.[0-9]{1,2})?|0)$

Regex validation on decimal

I am using the following regex to validate decimal numbers with dot .
/^[0-9]*\.?[0-9]*$/
It works fine for all the cases except the case 12.
Working Example:
12
12.2
10.222
12.
I want to throw validation error when user enters (12.): at least a digit after decimal point needs to be entered (like 12.1).
You can use this regex:
/^\d+(\.\d+)?$/
It will match whole number: 12, 1222
If there is a decimal point, then there must be at least 1 digit before and after the decimal point: 1.1, 34.2
These cases are not allowed: .43, 23.
Simply add one or more quantificator:
^[0-9]+(\.[0-9]+)?$

regular expression allows only decimal to enter

^(0)?[0-9]{0,}((\.){1}[0-9]{0,2}){0,1}$
The above regular expression allows me to enter numbers upto 4 decimal places with an optional 0 before the decimal(.) point. But it is not throwing error when I'm not entering numbers after decimal like 0., 12.,etc. !!!
Any guidance on this.
This is because you allow between 0 and 2 digits after the ..
Since you said you wanted between 1 and 4 digits, you probably want this:
^[0-9]*(\.[0-9]{1,4})?$
Note, I have replaced {0,} with * and {0,1} with ? as they are equivalent.
In addition, I've removed the {1}, as it matches one time per default.
The initial (0)? is also a bit redundant.
Beware, this regular expression matches .50. If you want to ensure that a number exists before the decimal point, change the first * to a + like so:
^[0-9]+(\.[0-9]{1,4})?$
To further simplify the regular expression, you can also replace [0-9] with \d in most regular expression engines, giving you:
^\d+(\.\d{1,4})?$
This regex could be simplified to the following:
^\d*(\.\d{1,4})?$
You can see it in action here: http://regexr.com?2vamh