I am new to regular expression , i want a regular expression which satisfies following reqs: User can enter a value in which the fractional part should not be more than 3 digits ,it can be less than 3 digits and the decimal part should not more than 1 digit.The number can be without decimal part in that case the digits should not more than 3.It is also possible to just have decimal part.Please help
Asked
Active
Viewed 506 times
1 Answers
0
Please try this,
^\d{0,3}(\.(?=\d)\d){0,1}$
This matches the followings,
123
1.2
12.3
123.4
I hope this helps.

KDS
- 137
- 1
- 10
-
thanks a lot !! i have modified the expression to suit my need of taking into consideration of either + or - sign. i have used the following expression : ^(\-?|\+?)\d{0,3}(\.(?=\d)\d){0,1}$. But am getting a crash when my using this line of code : static const boost::regex ex( "^(\-?|\+?)\d{0,3}(\.(?=\d)\d){0,1}$" ); Is it because of an exception ? please guide me – Sadaab Sep 03 '14 at 12:51
-
Well, if you need to include the + and - signs, I would use the original expresion above as follows, ^[\-|\+]\d{0,3}(\.(?=\d)\d){0,1}$ However, this might not be the reason for carsh. Appriciate if you vote for the answer. – KDS Sep 03 '14 at 15:57
-
thank you !! one last request...in the above given answer by you i want 1. , 12. , 123. conditions to be satisfied too.....i have been looking but no clear solution .It allows me to use .1 but not 1. etc . – Sadaab Sep 03 '14 at 16:30
-
I tried something like this "^\d{0,3}\.?(?<=\.)\d{0,1}$" but its failing for 1 , 12 , 123 . Please Help – Sadaab Sep 03 '14 at 16:47