Regular expression for text input - regex

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.

Related

Regex expression to prevent character in a floating number [duplicate]

I want to validate my currency field with regex. I want to allow the following pattern entries
1.23
1
.45
0.56
56.00
No comma should be allowed. I've tried \d+(\.\d\d) but it allows only first, fourth and fifth entries. \d+(?:\.\d\d+)? allows all but third one.
Use \d* instead of \d+ before the decimal to match zero or more digits. Also add anchors (^ and $) or else it will pass as long as there is any match available. This would also validate an empty string, so if necessary you can use a lookahead to make sure there is at least one digit:
^(?=.*\d)\d*(?:\.\d\d)?$
Regexes for floating-point numbers are a solved problem:
\d*\.?\d+
For at least two decimals:
(\d*\.\d)?\d+
To make it more comprehensible:
\d+|\d*\.\d{2,}
And for exactly two decimals:
\d+|\d*\.\d{2}
Depending on your language, don't forget to anchor the expression so that it must match the whole string.

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.

regex for integer or floating point number with two decimals

I want to validate my currency field with regex. I want to allow the following pattern entries
1.23
1
.45
0.56
56.00
No comma should be allowed. I've tried \d+(\.\d\d) but it allows only first, fourth and fifth entries. \d+(?:\.\d\d+)? allows all but third one.
Use \d* instead of \d+ before the decimal to match zero or more digits. Also add anchors (^ and $) or else it will pass as long as there is any match available. This would also validate an empty string, so if necessary you can use a lookahead to make sure there is at least one digit:
^(?=.*\d)\d*(?:\.\d\d)?$
Regexes for floating-point numbers are a solved problem:
\d*\.?\d+
For at least two decimals:
(\d*\.\d)?\d+
To make it more comprehensible:
\d+|\d*\.\d{2,}
And for exactly two decimals:
\d+|\d*\.\d{2}
Depending on your language, don't forget to anchor the expression so that it must match the whole string.

Regular expression for strict decimal numbers

I want to have a regexp to validate DECIMAL numbers:
The valid DECIMAL numbers are :
+123
123
-123
+123.0000
+123,123.999
.0234
+1123,233,34
invalid DECIMAL numbers are :
+1233+
-1233_
+12.233.33
Is there any one who can help on this
This is better:
^[+\-]?\d+(,\d{3})*(\.\d*)?$
Plus or minus, optionally, followed by any number of digits, followed optionally by any number of ,123 sections, followed optionally by a decimal and some more digits, this will handle anything except numbers with a leading decimal.
^[+\-]?(\d+(,\d{3})*(\.\d*)?|\.\d+)$
Adding that other option allows numbers with a leading decimal, which MUST be followed by digits.
Depends on your regular expression support.
^([-+])?(\d+)?(\.\d+)?$
Optional + or -, numeric, optional ',' followed by more numerics.
Will also match the empty string unfortunately.
I think this is about the simplest you can get:
^[+-]?\d*\.?\d+$
Digits are always required after the decimal point. Taking advantage of this fact simplifies the problem.
Update: At first I didn't notice that you also need to handle commas. Here is a simple version that allows commas to the left of the decimal point. Each comma must have at least one digit before and after it, but other than that no rule is enforced (which seems to be the point of your example data). Actually, it requires two digits after the final comma.
^[+-]?(\d+(,\d)?)*\.?\d+$
This works for all:
+123
123
-123
+123.0000
+123,123.999
.0234
+1123,233,34
check the demo:
^[+\-]?(\d+(,?\d)*(\.\d*)?|\.\d+)$
Regex Demo
You can use this
^([-+] ?)?[0-9]+(,[0-9]+)?$

Regex for certain numbers with decimal point

I am new at using the regex.
I want to allow strings with following format:
(Any number of digits).(two digits after .)
e.g. 11.34, 111.78, 132323.78
Can anybody please help me in this?
That would be \d+\.\d\d
\d means any digit
+ means one or more
\. means a literal dot.
EDIT
if you actually mean "two or more digits after" as Mu Mind suggests try the following:
\d+\.\d\d+
\d+\.\d\d
That will match 1 or more digits, then a dot, then 2 digits. (those last two digits must exist in this case.
Fixed: Extra dot