-1

I am trying to write a regex for the following condition:

Allow to enter up to 1 decimal place and if I start with integer 0; the next has to be decimal otherwise any other number entered will remove the 0(integer)

The regex that I have provided is /(^$)|(^[0-9]+(\.([0-9]{1})?)?$)/ it accepts 1 decimal place but I am unable to achieve the remaining part.

Edit 1

passed numbers --> 1.3

4.5

0.4

Failed numbers ---->

004

If number starts with 0, next character must be a decimal only Eg,

0.5 is a pass

005 is a failed

halfer
  • 19,824
  • 17
  • 99
  • 186
akash89
  • 881
  • 3
  • 12
  • 31
  • 2
    This is not clear, please provide the code you are using the regex in and a couple of sample inputs/expected outputs. – Wiktor Stribiżew Apr 29 '20 at 18:28
  • [This answer](https://stackoverflow.com/a/22397004/17300) is PHP, not Javascript, and for two decimals, but the regex principles are the same. – Stephen P Apr 29 '20 at 19:23
  • agree with Wiktor, this question _really_ needs more details. You say "upto 1 decimal place" — do you mean only one digit should be allowed after the (optional) decimal place, e.g. should `3.1416` be rejected? Is a bare decimal with nothing after it allowed, e.g. is `17.` ok? Is the only problem "the remaining part"? That is to say, is stripping off the leading zeros the only issue you have? Stripping off the leading `000` can be a separate concern from matching with the regex. `regex.test(string)` and `string.match(regex)` are different for different purposes, which are you using? – Stephen P Apr 29 '20 at 19:36
  • @StephenP - i have updated the question properly, please have a look. – akash89 Apr 30 '20 at 10:10

3 Answers3

0

try this:

^([1-9]+\d*.?\d?)|^(0.\d)

If not, see comments and try to better exemplify

Aks Jacoves
  • 849
  • 5
  • 9
  • i have updated the question properly, please have a look – akash89 Apr 30 '20 at 10:10
  • No details and very confusing your question. You speak for example of "removing" the first digit if it starts at 0 and the next is a number other than a decimal, but young man, you just say a regular expression and nothing else, a regular expression looks for patterns. As your question has no information and is only confused, what I can deduce is that the user will type values ​​in an input and you needed to put conditions for the values ​​entered (functions, methods, algorithms, logic, ...). My regular expression will * read * only decimal values ​​of type, 0.3, 2.4, 321.4, 0.4 – Aks Jacoves Apr 30 '20 at 13:27
  • Where are you going to use this regular expression? What is your algorithm about? What do you want to do? – Aks Jacoves Apr 30 '20 at 13:30
0

Try this regex:

^([0]{1}(\.[0-9]+))$|^([1-9]{1}|[1-9][0-9]+)$

ADJUSTED REGEX: I have now adjusted the regex as required: all numbers that start with 0 MUST follow a comma, e.g. 0.2 or 0.0002 is accepted. 02 or 00002 is NOT accepted

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
  • Your regex rejects decimals > 9 — OPs regex accepts them, for example `98.6`. OP says _"upto 1 decimal place"_ which usually means 1 significant digit _after_ the decimal place, but the question is unclear. – Stephen P Apr 29 '20 at 20:15
  • yes the question isn‘t clear... I have now changed the answer, but do not know if that is where you are looking for because the question is not clear – SwissCodeMen Apr 29 '20 at 20:55
  • i have updated the question properly, please have a look – akash89 Apr 30 '20 at 10:10
  • @akash89 I have edited the post... look whether it fits now ... – SwissCodeMen Apr 30 '20 at 11:23
0

OK, you want find integer number not start with 0, and find decimal? Your code is: /(^$)|(^[0-9]+(\.([0-9]{1})?)?$)/

Case 1: integer [1-9]\d*

Case 2: decimal

//match 5.0
patt = \d+\.\d+

//Match 5.1xxxx, 5.2xxx, etc.
patt = \d+\.[1-9]\d*

//match 5.0xxx
patt = \d+\.0\d+

//combine all of them
patt = ([1-9]\d*|\d+\.[1-9]\d*|\d+\.0\d+)

//Final
patt = /(^$)|(([1-9]\d*|\d+\.[1-9]\d*|\d+\.0\d+)?$)/

I will explain some pattern above:

\d == [0-9]     //match 1 character between 0 and 9
\d* == \d{0,}   //match 0 or more character
\d+ == \d{1,}   //match at least one character
Transamunos
  • 101
  • 4