number validaiton - javascript regex - regex

I've come up with this regular expression to validate a number which can have Maximum length-13 (including decimal points),Maximum no of decimal points-3,Maximum length of a whole number-12.
^(\d{1,12}([.]\d{1,1})?|\d{1,11}([.]\d{1,2})?|\d{1,10}([.]\d{1,3})?)$
Could anyone tell me if my approach is correct or give me a better solution?

This would also work:
^(?=.{1,13}$)(\d{1,12})(\.\d{1,3})?$
Uses positive look ahead to match the entire string length is ok.
Then it uses a group to match from 1 - 12 digits
Then there's an optional group to match a decimal followed by 1-3 digits.
Edited: Simplified since the rules don't allow a 13 digit integer-part

Related

What's an optimal regex for to represent a binary 32-bit decimal number?

Trying to fix Regex to match the following scenario:
Up to 32 bit after decimal
binary
No leading or trailing zeros
[0|1]*.?[0-1]+([0-1]{32})?
Regex should pass this: 1.001100101 but fail this: 1.0000000000000010
From your description, this regex should do what you want:
^1[01]*(?:\.[01]{0,31}1)?$
It looks for a leading 1, followed by some number of binary digits ([01]), and then an optional decimal part consisting of a decimal point (\.) followed by up to 31 binary digits ([01]{0,31}) and a trailing 1.
Demo on regex101
If you're looking for the numbers inside a longer string, replace ^ and $ with word breaks (\b).
Update based on comments
If you also want to match the values 0.0 and 1.0 (which do not meet the original criteria), you can add them as an alternation to the regex:
^(?:[01]\.0?|1[01]*(?:\.[01]{0,31}1)?)$
Demo on regex101
The shortest (and simplest) regex I can think of to accomplish this is:
^1\.[01]{0,31}?1$
This requires a 1. at the start, a 1 at the end, and also ensures a limit of 32 decimal places (including the final mandatory 1).
And can be seen on Regex101 here.

Regex for allowing numbers without leading and ending 0

I want except some numbers in different syntax and I am trying to find the best Regex for this task/match.
First some valid numbers:
0.01
0.2
0.38
45
165.6
52732.08
999999999.99
And here some invalid numbers:
.01
.2
.50
.85
45.
45.0
45.00
00045.0
124.60
000124.60
124,6
000053853.01
999.999.999,99
999999999,99
After several tests I have created the following Regex:
^[1-9]?\d{1,9}\.?\d{1,2}(?<!0)$
But I always struggling on the number: 000058723.01
Any ideas? Thanks.
You can use this regex:
^(?!0+\d)\d+(?:\.(?![1-9]*0+$)\d{1,2})?$
Or:
^(?:0+|[1-9]\d*)(?:\.(?![1-9]*0+$)\d{1,2})?$
RegEx Demo
Try this pattern:
^((?:0|[1-9]+)(?:\.(?:\d+?[1-9]|[1-9]))?)$
Demo
You accept four kinds of input:
A number with no decimal places and without leading zeroes: [1-9]\d*
Zero followed by a dot followed by digits (without trailing zeroes): 0\.\d*[1-9]
A decimal number without leading or trailing zeroes: [1-9]\d*\.\d*[1-9]
Zero: 0
Putting the four together:
^([1-9]\d*|0\.\d*[1-9]|[1-9]\d*\.\d*[1-9]|0)$
Here is a fixed version of your regex:
^(?!0{2,})\d+(?:\.\d{1,2}(?<!0))?$
Here, initial 2 or more zeros are not allowed with the lookahead (?!0{2,}), and the decimal part is made optional within a non-capturing group (?:\.\d{1,2}(?<!0))?.
See demo
In case you do not want to match 0, you can exclude this in the negative lookahead:
^(?!0{2,}|0$)\d+(?:\.\d{1,2}(?<!0))?$
^^
See Demo 2
A number with optional decimals is composed from two pieces: the integer part and the optional decimal part that starts with a dot.
The integer part is either zero (0) or a sequence of digits that start with 1..9 (no 0) and can continue with zero or more digits:
0|[1-9][0-9]*
If you need to impose an upper limit on the integer part's length then replace * with {,n} where n is the maximum allowed length minus 1.
The decimal part starts with a dot (.) followed by zero or more digits and followed by one of 1..9 (no 0 allowed at the end).
The expression is:
\.[0-9]*[1-9]
Now let's combine them:
^(0|[1-9][0-9]*)(\.[0-9]*[1-9])?$
What I added when I joined the pieces:
^ - match the start of the string; without this the regex matches 45.0 from 00045.0;
parentheses around the integer part because of the lower precedence of |;
parentheses around the decimal part, followed by ? to signal the entire decimal part is optional;
$ - match the end of the string to avoid matching 124.6 from 124.60.
Remarks
The above regex was designed to match your examples. However, please notice that most programming languages allow most or all of the numbers you put in the "invalid" section and use a dot (.) as decimal separator. And many languages provide library functions that are able to parse the numbers that use a comma (,) as decimal separator.
Numbers without integer part (.85), without digits after the dot (45.) ore with trailing zeros (45.0) are valid and are interpreted without ambiguity.
The only troublemaker is the leading zero (00045.0). For integer numbers, most of the times it is a signal that the number is represented in base 8 while for real numbers it is simply ignored.

Decimal Regex Constraint Matching (Four digits before optional decimal point and two after)

I need to figure out how to make my regex allow match correctly each time I type a number/decimal point. I want to limit the number of digits before and after the decimal point, which isnt too hard but i cant figure out how to allow the decimal point to match as well.
1 - match
12 - match
1234 - match
12345 - wrong
1234. - match
1234.1 - match
1234.12 - match
1234.123 - wrong
Other matched numbers
12.12
1.0
123.99
Edit:
So I want a max of 4 numbers before the decimal place and two after. Also the decimal place is optional.
The tricky part is that I want it to fail if the fifth character isn't a decimal point.
You need to specify your constraints better; I'm assuming you want a maximum of 4 before the dot and 2 after:
/^\d{1,4}(\.\d{0,2})?$/
edit: I added beginning and end of string matchers. Should work as you want now
You can use the following regex to select only those words that consists of digits and satisfying your condition.
/(?<=^|\s)\d{1,4}(?:\.\d{0,2})?(?=\s|$)/g
Positive lookahead and lookbehind are used to make sure that a whitespace is around the number.
DEMO
Debuggex Demo
Something like this will help
r'^\d{1,4}(\.\d{0,2})?$'
As you must be aware, \d represents a digit, . for the decimal point and {min_required,max_required}. Be sure to test your regular expression prior to using them here.

Regular expression for text input

I want users to be allowed to enter numbers, up to 3 digits before the decimal place, with an optional decimal place and a maximum of 2 digits after the optional decimal place.
I want it to match: 12, 123, 123.5, 123.55, 123.
I do not want it to match: abc, 1234, 123.555
What I have so far it:
^\d{0,3}(.?)\d{0,2}$
At the moment it is still matching 1234. I think I need to use the look behind operator somehow but I'm not sure how.
Thanks
Try this:
^\d{0,3}(?:\.\d{0,2})?$
Or better, to avoid just a .:
^(?:\d{1,3}(?:\.\d{0,2})?|\.\d{1,2})$
Specifically, note:
Escaping the dot, or it matches any character (except new lines), including more digits.
Made the whole decimal part optional, including the dot. That is - the decimal dot is not optional - it must be including if we are to match any digit from the decimal part.
Even if you have escaped the dot, ^\d{0,3}(\.?)\d{0,2}$ isn't correct. With the dot optional, it can match 12378: \d{0,3} matches 123, (\.?) doesn't match anything, and \d{0,2} matches 78.
Working example: http://rubular.com/r/OOw6Ucgdgq
What about this?
/^\d{0,2}(?:\d\.|\.\d|\d\.\d)?\d?$/
Maybe this (untested)
^(?=.*\d)\d{0,3}\.?(?<=\.)\d{0,2}$
Edit - the above is wrong.
#Kobi's answer is correct.
A lookahead could be added to his first version to insure a NOT just a dot or empty string.
^(?=.*\d)\d{0,3}(?:\.\d{0,2})?$
You have to put the combination of decimal point and the decimal numbers optional. In your regex, only the decimal number is optional. 1234 is accepted because 123 satisfy ^\d{0,3}, not existing decimal point satisfy (.?), and 4 satisfy \d{0,2}.
Kobi's answer provided you the corrected regex.

How to detect exact length in regex

I have two regular expressions that validate the values entered.
One that allows any length of Alpha-Numeric value:
#"^\s*(?<ALPHA>[A-Z0-9]+)\s*"
And the other only allows numerical values:
#"^\s*(?<NUM>[0-9]{10})"
How can I get a numerical string of the length of 11 not to be catched by the NUM regex.
I think what you're trying to say is that you don't want to allow any more than 10 digits. So, just add a $ at the end to specify the end of the regex.
Example: #"^\s*(?[0-9]{10})$"
Here's my original answer, but I think I read you too exact.
string myRegexString = `#"(?!(^\d{11}$)` ... your regex here ... )";
That reads "while ahead is not, start, 11 digits, end"
If it's single line, you could specify that your match must happen at the end of the line, like this in .net ...
^\s*([0-9]{10})\z
That will accept 1234567890 but reject 12345678901.
Do you mean you want to match up to 10 digits? Try this:
#"^\s*[0-9]{1,10}\s*$"
If you are trying to match only numbers that are 10 digits long, just add a trailing anchor using $, like this:
^\s*(?:[0-9]{10})\s*$
That will match any number that is exactly 10 digits long (with optional space on either side).
var pattern =/\b[0-9]{10}$\b/;
// the b modifier is used for boundary and $ is used for exact length
Match something non-numeric after the length 10 string. My regex-foo isn't that good, but I think you've got it setup there to catch a numeric string of exactly length 10, but since you don't match anything after that, a length 11 string would also match. Try matching beyond the end of the number and you'll be good.
This should match only 10 digits and allow arbitrary numbers of whitespaces before and after the digits.
Non-capturing version: (only matches, the matched digits are not stored)
^\s*(?:\d{10})\s*$
Capturing version: (the matched digits are available in subgroup 1, as $1 or \1)
^\s*(\d{10})\s*$
You could try alternation?
^\s*(?\d{1,10}|\d{12,})