2
pattern = new RegExp('([0-9]+([.][0-9]{1,2})?|[.][0-9]{1,2})');

it should accept 00.00, 0.0, 00.25, 00.36, 00.56,, 222.25, 222.25, 2222.25,

should not accept 000.25, 0000.25

3
  • 4
    Should it accept 00222.25? Commented Mar 16, 2019 at 13:45
  • Should it accept 02.25? Commented Mar 16, 2019 at 13:54
  • It's funny how regex questions on Stack Overflow precipitate a contest—a race—to see who can cook up a matching regex the fastest. Most of these questions are probably duplicates in one way or another, but we race anyway, because it's fun. Commented Mar 16, 2019 at 14:06

4 Answers 4

1

If your value can not start with a zero, you could use an alternation:

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

That will match:

  • ^ Start of the string
  • (?: Non capturing group
    • (?: Non capturing group
      • `0{1,2}|[1-9]\d* Match a zero 1-2 times OR digit 1-9 and 0+ times a digit
    • )? Close on capturing group and make it optional
    • \.\d{1,2} Match a dot and 1-2 digits
    • | Or
    • [1-9]\d* Match digit 1-9 followed by 0+ times a digit
  • ) Close non capturing group
  • $ End of the string

Regex101 demo

If you do want to allow a leading zero, you could add matching 0+ times a zero 0* before the second alternation:

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

Regex101 demo

Sign up to request clarification or add additional context in comments.

9 Comments

@MithunS So you mean like this? regex101.com/r/2DlnTA/3 or like this regex101.com/r/2DlnTA/4
@MithunS You could make the first part optional in that case regex101.com/r/2DlnTA/5
Where u have learnt these things? where can i learn this complex thing?
but one issue is i am not able to set this pattern in Angular Reactive Form Control :(
These are for example 2 great sites rexegg.com and regular-expressions.info
|
1

You can use this

^(?:(?!0{3,}).*)\.\d+$
  • ^ - Start of string.
  • (?:(?!0{3,}).*) -Condition to avoid more than 2 zeros at start.
  • \.\d+ - Matches . followed by one or more digit.
  • $ - End of string

Demo

Comments

1

Try this:

(?<![0-9.])((?:[0-9]{1,2}|[1-9][0-9]*)(?:[.][0-9]{1,2})?)(?![0-9.])

Note the anchors (?<!) and (?!). You can omit the anchors if you wish but the anchors will let your pattern match even if the line contains noise other than the number. The (?<!X) insists that X not precede the match. The (?!X) insists that X not follow the match.

[If you wanted to insist that X did indeed precede and/or follow, then you would instead anchor with (?<=X) and/or (?=X).]

Based on the tenor of your examples, my solution assumes that these are acceptable: 01.23; 00.23; 1.23. It assumes that these are not acceptable: 011.23; 1.234.

2 Comments

How will you limit Number of 0's before the '.' ?
By [0-9]{1,2}|... Note the 0.
0

Use this:

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

Explanation:

Before the dot \., It accepts either of the two (|):

  • ^[0-9] the code starts with any number between 0 and 9
  • [1-9]+ then, there are as many digits between 1 and 9 as you wants

or:

  • ^0{0,2} the code starts with at most two zeros

2 Comments

Could you please describe the conditions that make a string accepted/rejected?
It should be in a format from 00.00 to 5000.00. Should accept 0,0.0,00.00,.1,0.1,00.01,00.12,0125.25,4999.99,5000,5000.00 (with max length of 7 including dot[.]). Please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.