objective c - Regular Expression for validate price in decimal -
i unable find workaround regular expression input price in decimal. want:-
12345
12345.1
12345.12
12345.123
.123
0.123
i want restrict digits.
i created 1 not validating assumed
^([0-9]{1,5}|([0-9]{1,5}\.([0-9]{1,3})))$
also want know how above expression different one
^([0-9]{1,5}|([0-9].([0-9]{1,3})))$ thats working fine.
anyone explanation.
"i using nsregularexpression - objective c" if helps answer more precisely
- (ibaction)btntapped { nsregularexpression * regex = [nsregularexpression regularexpressionwithpattern: @"^\\d{1,5}([.]\\d{1,3})?|[.]\\d{1,3}$" options:nsregularexpressioncaseinsensitive error:&error]; if ([regex numberofmatchesinstring:txtinput.text options:0 range:nsmakerange(0, [txtinput.text length])]) nslog(@"matched : %@",txtinput.text); else nslog(@"not matched : %@",txtinput.text); }
"i doing in buttontap method".
this simple 1 should suit needs:
\d*[.]?\d+
"digits (\d+
) can preceded dot ([.]?
), can preceded digits (\d*
)."
since you're talking prices, neither scientific notation nor negative numbers necessary.
just point of interest, here's 1 used, scientific notation , negative numbers included:
[-+]?\d*[.]?\d+(?:[ee][-+]?\d+)?
for new requirements (cf. comments), can't specify how many digits want on first regex gave, since it's not way has been built.
this 1 should suit needs better:
\d{1,5}([.]\d{1,3})?|[.]\d{1,3}
"max 5 digits (\d{1,5}
) possibly followed ((...)?
) dot followed max 3 digits ([.]\d{1,3}
), or (|
) dot followed max 3 digits ([.]\d{1,3}
)".