yogapriya shanmugam
yogapriya shanmugam

Reputation: 99

Regex to accept only numeric input with whole and half decimal point

I have used the below regex but it accept all values after decimal point. I want only whole numbers ( eg: 12) and half decimal point (eg 12.5)

Regex regex = new Regex("[^0-9.]+"); I want the below behavior.

For example

  1. Valid numbers : 12, 12.5
  2. Invalid numbers 12.1, 12.8

Upvotes: 0

Views: 605

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520978

Try using this pattern:

\d+(?:\.5)?

This would match whole numbers, as well as numbers which half just a decimal component of 0.5. If you also want to allow for 0.0 decimal endings, then use:

\d+(?:\.[05])?

For your actual code, you may use:

Regex regex = new Regex("@\d+(?:\.5)?");

Upvotes: 1

Related Questions