Simple regular expression for a decimal with a precision of 2

What is the regular expression for a decimal with a precision of 2?

Valid examples:

123.12
2
56754
92929292929292.12
0.21
3.1

Invalid examples:

12.1232
2.23332
e666.76

The decimal point may be optional, and integers may also be included.

Answers


Valid regex tokens vary by implementation. The most generic form that I know of would be:

[0-9]+(\.[0-9][0-9]?)?

The most compact:

\d+(\.\d{1,2})?

Both assume that you must have both at least one digit before and one after the decimal place.

To require that the whole string is a number of this form, wrap the expression in start and end tags such as (in Perl's form):

^\d+(\.\d{1,2})?$

ADDED: Wrapped the fractional portion in ()? to make it optional. Be aware that this excludes forms such as "12." Including that would be more like ^\d+\.?\d{0,2}$.


Need Your Help

How to write hbm.xml for given three tables

java hibernate

Here I am trying to create hibernet mapping between three tables.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.