Lucas Dayries Lucas Dayries - 4 months ago 21
Java Question

Java regexp for textfield matching

So I'm working with Java regexp and I'm having a bit of troubles to create one. I must get a regexp that matches numbers between 0.0 and 359.9 with always exactly one decimal, which isn't super complicated, but I'm doing a regexp for a textfield meaning that if the expression doesn't match at ever character, I can't type further.

What it means is that basically 3 35 359 359. 359.9 must all match for me to type in the field.

I've been trying stuff around with

\d(\.\d)?
but that doesn't match every step of the way and
\d(\.)?(\d)?
allows it to start with a zero. I can't get really further with what I know of regexp and I would appreciate any help.

Answer

Try this:

^(?:3[0-5]\d|[12]\d{2}|\d{1,2})(?:\.\d?)?$

Demo: https://regex101.com/r/mW0lI5/3