Hey I need a regex that would allow following input
matches: 0.1,1.1, 0.001, 1.4,

in short number should be less than 2.00.

Thanks for help.

Dani AI

Generated

Short answer: restrict the integer (left-of-decimal) part to exactly 0 or 1, and use anchors so the whole string is validated. saw things like 0000 because earlier suggestions allowed any number of digits before the dot; was on the right track but did not enforce a single-digit integer part or full-string anchors.

If you require a decimal point and at least one digit after it (e.g., 0.1, 1.4, 0.001):

^(?:0|1)\.\d+$

If an integer without a fractional part should also be allowed (e.g., 0, 1, or 0.5):

^(?:0|1)(?:\.\d+)?$

Notes and pitfalls:

  • Anchors (^ and $) are important so partial matches like 120.5 do not slip through.
  • These patterns disallow leading multi-digit integers such as 01 or 00.
  • If you need true numeric comparison (handle rounding, leading zeros, locale decimal separators, or enforce a strict numeric range), prefer parsing and comparing rather than only using regex. Example in C#:
using System.Globalization;

if (decimal.TryParse(input, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var v) && v < 2m)
{
    // valid
}

This parsing approach is more robust for production validation.

Recommended Answers

All 4 Replies

Like this:

[0-9]+\.+[0-9]{3}
the numer in the {} is the number that you want after the dot.

didnt help. It allows 0000 and 121212 too. which is not correct.

didnt help. It allows 0000 and 121212 too. which is not correct.

Next time say exactly the range that you need:
[0-9]{1}+\.+[0-9]{3}

Next time say exactly the range that you need:
[0-9]{1}+\.+[0-9]{3}

It still allows 11111, 00000, even 222222. Is there anyway we could specify 0 or 1 before decimal point and anything else after .(point), the decimal part.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.