Colton Seal
Colton Seal

Reputation: 379

Ruby regex for whole number

Cant seem to figure out a regex to match any number that is in this format

1.0 or 100.0, basically anything that is a whole number. Any ideas?

Upvotes: 0

Views: 212

Answers (1)

Federico Piazza
Federico Piazza

Reputation: 30985

You can use a regex like this:

^\d+\.\d$

This will allow numbers with one decimal.

On the other hand, if you want to allow number without decimal too, you can use:

^\d+(\.\d)?$

And if for some reason you want to allow multiple decimals to above regex, you can use:

^\d+(\.\d+)?$

You can use something like this:

/^\d+\.\d$/ =~ '100.1'
or
/^\d+\.\d$/.match('100.1')

For more details about Ruby regex take a look at the documentation

Upvotes: 2

Related Questions