Regular Expressions Match any float


Example

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

This will match any signed float, if you don't want signs or are parsing an equation remove [\+\-]? so you have \d+(\.\d+)?

Explanation:

  • \d+ matches any integer
  • ()? means the contents of the parentheses are optional but always have to appear together
  • '\.' matches '.', we have to escape this since '.' normally matches any character

So this expression will match

5
+5
-5
5.5
+5.5
-5.5