Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help with Regex - need up to 3 number after the dot (.)

Tags:

c#

regex

I need help with a regex matching a number. I need up to 3 digits after the dot (.):

12345    ok
12       ok
12.1     ok
12.12    ok
12.123   ok
12.1234  Error
1.123456 Error

how to do it? Thanks in advance.

like image 920
Gold Avatar asked Mar 04 '10 08:03

Gold


3 Answers

\d+(?:\.\d{1,3})?

Explanation:

\d+        # multiple digits
(?:        # start non-capturing group  
  \.       # a dot
  \d{1,3}  # 1-3 digits
)?         # end non-capturing group, made optional
like image 193
Tomalak Avatar answered Oct 02 '22 13:10

Tomalak


^\d+(\.\d{1,3})?$
like image 36
polygenelubricants Avatar answered Oct 02 '22 13:10

polygenelubricants


You can try:

^\d+|(\d+\.\d{1,3})$
  • \d - Single digit
  • \d+ - one or more digits, that is a number.
  • \. - dot is a metachar..so to match a literal dot, you'll need to escape it.
  • {1,3} - between 1 to 3 (both inclusive) repetitions of the previous thing.
  • ^ and $ - Anchors so that we match entire thing not just part of something.
like image 25
codaddict Avatar answered Oct 02 '22 13:10

codaddict