user846445
user846445

Reputation: 231

Ruby regex to match decimal number with percentage and only decimal numbers

The ruby regex expression (\d*[,]?\d*[.]\d+) matches both 45.00 and 45.00%. How to tune this regex that it matches only 45.00 and 45.00%

Upvotes: 1

Views: 1263

Answers (3)

Alin P.
Alin P.

Reputation: 44346

Just add a negative lookahead and make the decimal matching possessive:

\d*[,]?\d*[.]\d++(?!%)

Demo

Upvotes: 3

pguardiario
pguardiario

Reputation: 54984

I would clean that up to:

\d[\d,.]*+(?!%)

Upvotes: 0

nPn
nPn

Reputation: 16728

I am not sure exactly what you are looking for either. You can try this, it will match both 45.00 and 45.00%

(\d*[,]?\d*\.\d+[%]?)

But the best way to test regex's is with Rubular

You can enter several lines of test data and then check that your regex matches what you expect

Upvotes: 0

Related Questions