0

I am trying to validate a text field that accepts number like 10.99, 1.99, 1, 10, 21.

\d{0,2}\.\d{1,2}

Above expression is only passing values such as 10.99, 11.99,1.99, but I want something that would satisfy my requirement.

  • 2
    Make the decimal part optional. `\d{0,2}(\.\d{1,2})?`. Also are 1 digit decimals allowed? – chris85 Mar 27 '17 at 19:00
  • http://stackoverflow.com/questions/14550526/regex-for-both-integer-and-float – Youcef LAIDANI Mar 27 '17 at 19:04
  • yes! any number from 1-99 ... it could be 1.9, 2.99, 34, or 43.98 –  Mar 27 '17 at 19:05
  • 2
    @chris85 That would allow an empty string. – Andreas Mar 27 '17 at 19:05
  • 1
    It really depends on what kind of float value you want to allow. Note that Java allows all of the following `1`, `1.`, `1.0`, `0.1`, `.1`. If you also want to be that lenient, i.e. allow `.` without requiring digits on both sides, you want something like this: `\d{1,2}\.?|\d{0,2}\.\d{1,2}`, though maybe `\d{1,2}(?:\.\d{0,2})?|\.\d{1,2}` is better for performance of the regex. – Andreas Mar 27 '17 at 19:09
  • @LearningInProgress If it's a floating point, should it have exactly 2 decimal places? If not, what is the range of valid decimal places? And should there be at most 2 digits before the decimal point? – Bohemian Mar 27 '17 at 19:20
  • @Bohemian yes. maximum decimal places and maximum 2 digits before the decimal. –  Mar 27 '17 at 19:23

5 Answers5

3

Try this:

^\d{1,2}(\.\d{1,2})?$
  • ^ - Match the start of string
  • \d{1,2} - Must contains at least 1 digit at most 2 digits
  • (\.\d{1,2}) - When decimal points occur must have a . with at least 1 and at most 2 digits
  • ? - can have zero to 1 times
  • $ - Match the end of string
Bohemian
  • 412,405
  • 93
  • 575
  • 722
J.Chim
  • 41
  • 2
  • `^` (and `$`) are not required if using [`matches()`](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#matches--) instead of [`find()`](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#find--), a likely scenario for this question. Many other languages don't have that distinction, but Java does. – Andreas Mar 27 '17 at 19:14
1

First \d{0,2} does not seem to fit your requirement as in that case it will be valid for no number as well. It will give you the correct output but logically it does not mean to check no number in your string so you can change it to \d{1,2}

Now, in regex ? is for making things optional, you can use it with individual expression like below:

\d{1,2}\.?\d{0,2}

or you can use it on the combined expression like below

\d{1,2}(\.\d{1,2})?

You can also refer below list for further queries:

abc…    Letters
123…    Digits
\d  Any Digit
\D  Any Non-digit character
.   Any Character
\.  Period
[abc]   Only a, b, or c
[^abc]  Not a, b, nor c
[a-z]   Characters a to z
[0-9]   Numbers 0 to 9
\w  Any Alphanumeric character
\W  Any Non-alphanumeric character
{m} m Repetitions
{m,n}   m to n Repetitions
*   Zero or more repetitions
+   One or more repetitions
?   Optional character
\s  Any Whitespace
\S  Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*)    Capture all
(abc|def)   Matches abc or def

Useful link : https://regexone.com/

Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
1

Assuming you don't want to allow edge cases like 00, and want at least 1 and at most 2 decimal places after the point mark:

^(?!00)\d\d?(\.\d\d?)?$

This precludes a required digit before the decimal point, ie ".12" would not match (you would have to enter "0.12", which is best practice).

If you're using String#matches(), you can drop the leading/trailing ^ and $, because that method must to match the entire string to return true.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Can you try using this :

(\d{1,2}\.\d{1,2})|(\d{1,2})

Here is a Demo, you can check also simple program


You have two parts or two groups one to check the float numbers #.#, #.##, ##.##, ##.# and the second group to check the integer #, ##, so we can use the or |, float|integer

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

I think patterns of this type are best handled with alteration:

/^\s*([-+]?[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?)$ #float
|   # or
^(\d{1,2})$  # 2 digit int/mx

Demo

dawg
  • 98,345
  • 23
  • 131
  • 206