Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for positive float numbers

Tags:

For example:
10
0.1
1.23234
123.123
0.000001
1.000
.3

And wrong examples:
0001.2
-12
-1.01
+2.3

EDIT: standart JavaScript regex.

like image 353
Sergey Metlov Avatar asked May 17 '11 10:05

Sergey Metlov


People also ask

What type of numbers are represented by following regular expression 0 9 ]+ 0 9 ]+?

[0-9]+|[0-9]+). This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or that is followed by one or more digits (an integer).

How do you specify in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What is question mark in regex?

A question mark ( ? ) immediately following a character means match zero or one instance of this character . This means that the regex Great!? will match Great or Great! .


1 Answers

Try this here

^(?:[1-9]\d*|0)?(?:\.\d+)?$ 

See it here online on Regexr

If matching the empty string is not wanted, then you can add a length check to your regex like

^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$ 

The positive lookahead (?=.+) ensures that there is at least 1 character

like image 104
stema Avatar answered Sep 28 '22 03:09

stema