0

I am trying to validate a string with following allowed conditions

string can be signed or unsigned numbers with dot(.). for example

10 or .3 or 1. or 1.1 or +5 or -5 or +.3 or -1. etc

below link is almost a close answer but fails on "+.3" Regular Expression for whole numbers and integers?

"[+-]?(?<!\.)\b[0-9]+\b(?!\.[0-9])"
Community
  • 1
  • 1

2 Answers2

0

One solution may be:

[+-]?(((\d+\.?)|(\.?\d+))\d*)

Here [+-]? checks for the optional sign at the beginning, (\d+\.?) and (\.?\d+) will catch digit followed by . or . followed by digits (and covers the cases that contain a .), and finally \d* takes care of the trailing digits, if there is any.

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
-1

if you want to exclude strings like 000000.12 this would work I beleive [+-]?(0|[1-9]\d*)?(\.[0-9]+)?

d_z
  • 688
  • 3
  • 11