2

Could somebody help me with regex? Basically, I would like a regex which matches with decimal numbers.

Allowed types:

12
1.3234
0.3423434
23423.12

Not allowed types:

0012
12.324.12
01.2332
.12
121212.

Thanks for your help in advance!

Regards!

1
  • I have updated my answer to not match a single or zeroes only including the decimal part ^(?:0\.\d*[1-9]\d*|[1-9]\d*(?:\.\d+)?)$ demo Commented Mar 21, 2019 at 7:38

3 Answers 3

2

This regex should do your job.

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

Demo

  • ^ - Start of string
  • (?: - Beginning of a non-capture group
  • [1-9][0-9]* - Matches a number not starting with a zero
  • |0 - Or allows matching only a zero to allow numbers like 0.3423434 or 0.1
  • (?:\.[0-9]+)? - Optionally allows a decimal part in the number
  • $ - End of string

Edit: Based on Allan's suggestion to use \d instead of [0-9]

In case your input only contains English numbers, or when you only intend to match English numbers, then using [0-9] should be preferred for reasons like better performance and wide supported of it across regex engines.

But in case your input contains digits from other languages like Hindi etc. and you want to match any digits, then \d should be preferred instead of [0-9]

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

3 Comments

Nice answer! ^(?:[1-9]\d*|0)(?:\.\d+)?$ is even better if \d is recognized in OP language
@Allan: Thanks Allan and yes \d should be used if one is intending to also match Unicode digits in languages other than English like Hindi or . But instead of one only expects to match only English numbers 0 to 9 then using [0-9] give relatively better performance as then it just checks for 0 to 9 English number literals only. So up to OP based upon his input. And thanks again for suggestion, which allowed me to clarify the reason of using [0-9]. Let me update that in my post to make it clear.
The solution is working, however, I would like to make it more strict. What do I need to add to not allow the '0' value?
0

That would be

^[[:digit:]]+(\.[[:digit:]]+)?$

Beginning of the string, then one or more digits, then (optionally) a dot and one or more digits, then the end of the string.

If you want to cater for the sign, add [+-]? after the ^.

1 Comment

This regex does match 0012 and 01.2332... It shouldn't... You need to add constraint on the first digit encountered
0

You could use an alternation

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

Regex demo

Explanation

  • ^ Start of string
  • (?: Non capturing group
    • 0\. Match a single zero followed by a dot
    • \d*[1-9]\d* Match 0+ digits, a digit 1-9 and again 0+ digits t prevent only zeroes to match
    • | Or
    • [1-9]\d* Match a digit 1-9 followed by 0+ digits
    • (?:\.\d+)?) Optional part to match a dot and 1+ digits
  • ) Close non capturing group

Comments

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.