tags:

views:

62

answers:

2

Can some please help me with regular expression for height in cm ( eg. 170.25)(after dot only 2 character), weight in kg ( eg. 57.750) (after dot only 3 character),both numeric.

this kind of value format should be accepted

Height: 57,57.55 or 150,150.55

Weight: 77,77.55,77.565 or 150,150.77,150.777

+1  A: 

The basic regular expressions are quite straight forward:

\d{2,3}\.\d{,2}

will match the height (any number of decimal digits followed by a decimal point followed by exactly 2 digits and:

\d{2,3}\.\d{,3}

will match the weight. Having said that, depending on where the input comes from they will match other things too (e.g. bits of ip addresses) so I would add more context to the expression. You should also check how exact weights are represented. Is 57 kg shown as 57, 57.0, or 57.000 (the expression above will only match the latter.

Andrew Walker
here user will input the height and weight the value can beHeight: 57,57.55 or 150,150.55Weight: 77,77.55,77.565 or 150,150.77,150.777
deepu
Ok, I've updated the regular expressions in the answer. This (obviously) won't be able to tell the difference between height or weight (is 150.55 a weight or a height) so you'll need to know this by searching for the units or knowing something else about the input.
Andrew Walker
Lol, dude. This requires some mind reading hardware or - easier to construct - hardware reading the intention from an eye pupil ;PPFrom the heart rate you could also conclude, if someone lies about their age/weight.
AOI Karasu
@Andrew Walker that expression not sutiable for height format like 55,55.5 etc...it only accepts 55.55, just thnk abt the format, the units all not need to be specified... here if user types any other formats it should be rejected..only the specified format should be accpeted
deepu
+1  A: 
\d+(\.\d{1,3})?

should work

agsamek
@agsamek it work for the case 55.55 like after (.) cases but the first section ie max of only 3 values can come before dot(.)
deepu