user7917367
user7917367

Reputation:

Regex expression for empty string that accept number with 1 place decimal?

I want a regular expression for empty string that accept number with 1 place decimal

Accepted value

[blank space]
1.1
2.1
3.1
22
12
0
empty string

Not accepted

q.q
1q
.0
.00

Tried

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

but it's no idea for blank space

Upvotes: 2

Views: 423

Answers (2)

user7011379
user7011379

Reputation:

TRY THIS

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

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521289

Try this regex:

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

You can use an alternation to either match an single space, or to match your number pattern for a whole number or a number with at most one digit past the decimal place. Note that empty string is also a valid input, because the entire pattern has the form ^(...)?$, which allows for no input. I removed {1,1}, since [0-9] implicitly means only one number.

Demo

Upvotes: 2

Related Questions