-1

I am working on a regex for prices in specific formats.

Valid inputs are:

  • Natural Numbers
  • Decimals(upto two places after decimal point) except 0.0 and 0.00
  • All the above criteria without leading zeroes (ex- 05 and 08.45 are invalid but 5 and 8.45 are valid)

Examples of valid test cases

1
11
0.10
0.01
1.1
1.00
11.11

Examples of invalid test cases

0
0.
0.0
0.00
0.000
01
001
1.
1.111
1111.1111
00.1
00.11
01.11

Here's what I have tried but can't get it to work

/^(((?!0)\d+)(.\d{1,2})?)|(0\.(([1-9]\d)|(0[1-9])))$/gm

See Demo #1.

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

See Demo #2.

The solution in Regular Expression for Currency doesn't work for me as it validates 00.11 successfully.

Edit : I guess with some fiddling, I got it right this time. Just changed the [1-9] to [0-9] and it worked.

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

See Demo #3.

All thanks to Wiktor Stribiżew for helping this newbie out. Much appreciated sir.

  • 1
    `^(?![0.]*$|0+[1-9])\d+(?:\.\d{0,2})?$`? See https://regex101.com/r/uFSPP5/1 – Wiktor Stribiżew May 27 '21 at 17:29
  • It's accepting "1." too as shown in your link sir. Thanks for your efforts though. – Debasis Rath May 27 '21 at 17:33
  • It is not clear what you need to accept and what not. Try `^(?![0.]*$|0+[1-9])\d+(?:\.\d{1,2})?$` then. See https://regex101.com/r/uFSPP5/2. Also, please check the duplicate question and update your question with your findings (whether the solutions there work for you or not). – Wiktor Stribiżew May 27 '21 at 17:35
  • Sorry for troubling again but it also accepts "000000.80", "00.80" etc which are still invalid. Kindly have a look if possible https://regex101.com/r/uFSPP5/3 – Debasis Rath May 27 '21 at 19:02
  • Then make sure you use a more specific title and add all valid/invalid test cases to the question. You must also explain why that duplicate is not helpful. – Wiktor Stribiżew May 27 '21 at 19:12
  • Sir, the duplicate also marks the input pointed out by me earlier as valid. For example, it marks "00.80" as valid. I will try and update this question according to my specific requirement though. Thanks and sorry for my bad english. – Debasis Rath May 27 '21 at 19:26
  • @WiktorStribiżew Updated my question as per your advice sir. – Debasis Rath May 27 '21 at 19:47
  • You can also use `^(?![0.]*$|0\B)\d+(?:\.\d{1,2})?$`, see [this demo](https://regex101.com/r/uFSPP5/5). – Wiktor Stribiżew May 30 '21 at 18:37

1 Answers1

0

Use

^(?!0+(?:\.0+)?$|0+\d)\d+(?:\.\d{1,2})?$

See proof

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    0+                       '0' (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      \.                       '.'
--------------------------------------------------------------------------------
      0+                       '0' (1 or more times (matching the
                               most amount possible))
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    0+                       '0' (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d{1,2}                  digits (0-9) (between 1 and 2 times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37