1

I want a regex for the following given pattern:

{anynumber} will return true.

+{anynumber} will return true.

-{anynumber} will return false.

{anynumber}.5 OR {anynumber}.50 OR {anynumber}.500 and similar pattern will return true (i.e. any number of zeros after .5).

+{anynumber}.5 / +{anynumber}.50 / +{anynumber}.500 and similar pattern will return true.

-{anynumber}.5 / -{anynumber}.50 / -{anynumber}.500 and similar pattern will return false.

+{anynumber}.6 / +{anynumber}.75 / +{anynumber}.205 and similar pattern will return false (i.e. after decimal point only 5 is allowed at the first place with any or no number of zeros afterwards).

No limitations of length of numbers.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Swarup Saha
  • 895
  • 2
  • 11
  • 23

1 Answers1

0

Here you are:

^(?:(?: \/ )?[+]?[0-9]+(?:\.50*\b)?)+$

And here is a demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks @stribizhev, 21.55555 should return false. As I want only one 5 at the very first place. No other number will be allowed after first place except any / no number of zero(s) Could you please modify the regex and post the correct one? – Swarup Saha Mar 24 '15 at 09:13
  • Hi @SwarupSaha, I updated the answer by adding `\b` to the non-capturing group. Now, it should not match `21.55555`. – Wiktor Stribiżew Mar 24 '15 at 09:15
  • If I add one more restriction like I want to allow {anynumber}.0 OR {anynumber}.00 (one or multiple time) to return true. What needs to be done? – Swarup Saha Mar 24 '15 at 09:39
  • You'll need to add an alternative `(?:50*|0)`, e.g. `^(?:(?: \/ )?[+]?[0-9]+(?:\.(?:50*|0)\b)?)+$` - see https://regex101.com/r/hT2sU2/4 – Wiktor Stribiżew Mar 24 '15 at 09:44
  • 10.00 is returning false while 10.0 is returning true.. Could you please have a look and update me? – Swarup Saha Mar 24 '15 at 09:55
  • Here you are, a `+` quantifier should be added to `0`: `^(?:(?: \/ )?[+]?[0-9]+(?:\.(?:50*|0+)\b)?)+$`, see https://regex101.com/r/hT2sU2/5. – Wiktor Stribiżew Mar 24 '15 at 09:58